我有在最左邊的列和其他列的正向曲線(以下DF1)熊貓列名
DF1一些代碼的DF:
code tp1 tp2 tp3 tp4 tp5 tp6 \
0 1111 0.000000 0.000000 0.018714 0.127218 0.070055 0.084065
1 222 0.000000 0.000000 0.000418 0.000000 0.017540 0.003015
2 333 1.146815 1.305678 0.384918 0.688284 0.000000 0.000000
3 444 0.000000 0.000000 1.838797 0.000000 0.000000 0.000000
4 555 27.190002 27.134837 24.137560 17.739465 11.990806 8.631395
5 666 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
tp7 tp8 tp9 tp10
0 0.019707 0.000000 0.000000 0.000000
1 6.594860 10.535905 15.697232 21.035824
2 0.000000 0.000000 0.000000 0.000000
3 0.000000 0.000000 0.000000 0.000000
4 7.476197 6.461532 5.570051 4.730345
5 0.000000 0.000068 0.000000 0.000000
我想輸出爲具有最後一個數字(+ ve或-ve)的單元(對於每個代碼)的列名稱的3列df(下面的df2),其後僅有0。第二列(tp_with_max_num
)將具有最大數目的列名稱。
DF2:
code max_tp tp_with_max_num
0 1111 tp7 tp4
1 222 tp10 tp10
2 333 tp4 tp2
3 444 tp3 tp3
4 555 tp10 tp1
5 666 tp8 tp8
使用此:name of column, that contains the max value 我能得到第3列:
input_df['tp_with_max_num'] = input_df.ix[0:6,1:].apply(lambda x: input_df.columns[1:][x == x.max()][0], axis=1)
我無法解決在DF2第2列....
如此優雅!謝謝vm!現在試着去理解剛纔發生的事情...... – spiff
但是,你必須稍微改變它以使用負數。在問題示例中沒有任何內容,但稍後可能會有一些內容,因此您可以在執行'cumsum'之前調用'abs'。 – jotasi