第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Pandas 的循環(huán)中創(chuàng)建索引列名

如何在 Pandas 的循環(huán)中創(chuàng)建索引列名

浮云間 2021-12-26 15:39:18
我需要?jiǎng)?chuàng)建一個(gè)函數(shù)來為新數(shù)據(jù)集自動(dòng)分配一些新列名。原因是我需要某種方式將我的預(yù)測(cè)綁定到我的初始數(shù)據(jù)集。Sklearn 沒有任何簡(jiǎn)單的方法可以做到這一點(diǎn)。我已經(jīng)嘗試創(chuàng)建一個(gè)函數(shù),但是它只打印一個(gè)錯(cuò)誤:ValueError: 傳遞值的形狀是 (1440, 90),索引意味著 (1440, 1)這意味著我在定義新列名時(shí)沒有正確使用該函數(shù)。def column_printing(x):    i=0    for i in range(x):        print('prediction', i+1)        i+1resultTestDataset = pd.DataFrame(y_test, columns=[column_printing(predict_length)])讓我們說:predict_length = 3y_test = [5,6,7],         [3,2,1]我想要一個(gè)如下所示的數(shù)據(jù)框:prediction1, prediction2, prediction35,6,7 3,2,1
查看完整描述

2 回答

?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果我可以假設(shè)您y_test是numpy array.


您可以使用以下內(nèi)容:


predict_length = 3

y_test = np.reshape(np.array([5,6,7,3,2,1]), (2,3))


df = pd.DataFrame(y_test, columns=['predicition{}'.format(x+1) for x in range(predict_length)])


print(df)

   predicition1  predicition2  predicition3

0             5             6             7

1             3             2             1

如果你的python版本>=3.6,我們可以使用 f-strings


predict_length = 3

y_test = np.reshape(np.array([5,6,7,3,2,1]), (2,3))


df = pd.DataFrame(y_test, columns=[f'predicition{x+1}' for x in range(predict_length)])


print(df)

   predicition1  predicition2  predicition3

0             5             6             7

1             3             2             1


查看完整回答
反對(duì) 回復(fù) 2021-12-26
?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

這會(huì)有所幫助


# a simple function to do the column name creation

column_print = lambda col_len : ['prediction' + str(i+1) for i in range(col_len)]


y_test = [5,6,7],[3,2,1]

y_test = np.array(y_test) # convert y_test to a numpy array so you can use the shape 

#method

size_y = y_test.shape

resultTestDataset = pd.DataFrame(y_test, columns=column_print(size_y[1]))

print(resultTestDataset)


查看完整回答
反對(duì) 回復(fù) 2021-12-26
  • 2 回答
  • 0 關(guān)注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)