2017-03-01 88 views
2

我正在尋找更多的編程方法來創建多個新列作爲熊貓數據框中現有列的功能。動態創建新列作爲熊貓現有列的功能

我有14欄Level_2 - Level_15。我想迭代地創建14列新列,總計列2-15,然後3-15,然後4-15等等。

現在我的代碼看起來是這樣的

cols['2_sum'] = cols.Level_2 + cols.Level_3 + cols.Level_4 + cols.Level_5 + cols.Level_6 + cols.Level_7 + cols.Level_8 + cols.Level_9 + cols.Level_10 + cols.Level_11 + cols.Level_12 + cols.Level_13 + cols.Level_14 + cols.Level_15 
cols['3_sum'] = cols.Level_3 + cols.Level_4 + cols.Level_5 + cols.Level_6 + cols.Level_7 + cols.Level_8 + cols.Level_9 + cols.Level_10 + cols.Level_11 + cols.Level_12 + cols.Level_13 + cols.Level_14 + cols.Level_15 
cols['4_sum'] = cols.Level_4 + cols.Level_5 + cols.Level_6 + cols.Level_7 + cols.Level_8 + cols.Level_9 + cols.Level_10 + cols.Level_11 + cols.Level_12 + cols.Level_13 + cols.Level_14 + cols.Level_15 

是否有更多的熊貓或Python的方式做到這一點?

謝謝!

回答

1

您可以創建列列表

cols= list(cols) 
cols['2_sum'] = cols[cols].sum(axis = 1) 
cols['3_sum'] = cols['2_sum'] - cols['Level_2'] 
cols['4_sum'] = cols['3_sum'] - cols['Level_3'] 
2

下面是一個例子:

的樣本數據:

In [147]: df = pd.DataFrame(np.random.rand(3, 15), 
    ...:     columns=['ID'] + ['Level_{}'.format(x) for x in range(2, 16)]) 
    ...: 

In [148]: df 
Out[148]: 
     ID Level_2 Level_3 Level_4 Level_5 Level_6 Level_7 Level_8 Level_9 Level_10 Level_11 \ 
0 0.851407 0.957810 0.204217 0.848265 0.168324 0.010265 0.191499 0.787552 0.648678 0.424462 0.038888 
1 0.354270 0.442843 0.631624 0.081120 0.357300 0.211621 0.177321 0.316312 0.836935 0.445603 0.267165 
2 0.998240 0.341875 0.590768 0.475935 0.071915 0.720590 0.041327 0.926167 0.671880 0.516845 0.450720 

    Level_12 Level_13 Level_14 Level_15 
0 0.465109 0.508491 0.282262 0.848373 
1 0.205415 0.399493 0.537186 0.774417 
2 0.131734 0.554596 0.253658 0.104193 

解決方案:

In [149]: for n in range(15, 1, -1): 
    ...:  df['{}_sum'.format(15-n+2)] = df.filter(regex=r'Level_\d+').iloc[:, :n].sum(1) 
    ...: 

結果:

In [150]: df 
Out[150]: 
     ID Level_2 Level_3 Level_4 Level_5 Level_6 Level_7 Level_8 Level_9 Level_10 ...  \ 
0 0.851407 0.957810 0.204217 0.848265 0.168324 0.010265 0.191499 0.787552 0.648678 0.424462 ... 
1 0.354270 0.442843 0.631624 0.081120 0.357300 0.211621 0.177321 0.316312 0.836935 0.445603 ... 
2 0.998240 0.341875 0.590768 0.475935 0.071915 0.720590 0.041327 0.926167 0.671880 0.516845 ... 

     6_sum  7_sum  8_sum  9_sum 10_sum 11_sum 12_sum 13_sum 14_sum 15_sum 
0 4.745067 4.279958 4.241070 3.816608 3.167931 2.380379 2.188880 2.178615 2.010292 1.162027 
1 3.973259 3.767844 3.500679 3.055076 2.218140 1.901828 1.724508 1.512887 1.155587 1.074468 
2 4.939755 4.808021 4.357301 3.840456 3.168576 2.242409 2.201082 1.480492 1.408577 0.932643 

[3 rows x 29 columns] 
+0

是的,這是非常接近。但是它每次總結所有列,我需要跳過每個循環中的第一列;總結列2-15創建2_sum,然後總結列3-15創建3_sum等 –

1

希望,它可以幫助你

ColsListName = ['3_sum' ... ,'14_sum'] 
ColsListLevel = ['Level_2','Level_3' ... ,'Level_15'] 
sumCols = cols.Level_2 + cols.Level_3 + cols.Level_4 + cols.Level_5 + cols.Level_6 + cols.Level_7 + cols.Level_8 + cols.Level_9 + cols.Level_10 + cols.Level_11 + cols.Level_12 + cols.Level_13 + cols.Level_14 + cols.Level_15 
cols['2_sum'] = sumCols 
for i in range(len(ColsListLevel)) : 
    cols[ColsListName [i]] = sumCols - cols.ColsListLevel [i] 
0
import pandas as pd 
import numpy as np 
np.random.seed(1) 
cols = pd.DataFrame(np.random.rand(2, 14), 
        columns=['Level_'+str(i) for i in range(2, 16)]) 

現在數據框的樣子:

Level_2  Level_3  Level_4  Level_5  Level_6  Level_7  Level_8  Level_9  Level_10 Level_11 Level_12 Level_13 Level_14 Level_15 2_sum 
0 0.199666 0.285152 0.598139 0.602477 0.004284 0.874587 0.263949 0.527301 0.306443 0.282778 0.181330 0.280506 0.456637 0.998124 5.861371 
1 0.279320 0.508074 0.435350 0.816866 0.691988 0.179261 0.134478 0.949185 0.867022 0.410112 0.139481 0.537539 0.042163 0.366138 6.356977 

然後:

for i in range(2, 15): 
    cols[str(i)+'_sum'] = cols.loc[:, 'Level_'+str(i):'Level_15'].sum(axis=1) 
cols 

我想這就是你想。

+0

是的,這是非常接近。但是它每次總結所有列,我需要跳過每個循環中的第一列;總結2-15列創建2_sum,然後總結列3-15創建3_sum等。 –

+0

我不太確定我理解。如果您的意思是「第2列」中的「Level_2」,則是結果。如果你的意思是「列2」中的「第二列」,由於DataFrame'cols'還沒有包含「ID」列,我認爲如果包含「ID」,它將起作用。 – gepcel