将值添加到数据框列中的最大值

时间:2019-01-12 21:09:45

标签: python pandas

我有一个数据框:

Region    |     A      |       B     |      C     |    Total
===============================================================
Africa    |   100.10   |    20.135   |    10.02   |    130.255
---------------------------------------------------------------
Europe    |   200.35   |    50.102   |    16.35   |    266.802
---------------------------------------------------------------
India     |   30.98    |    150.18   |    12.63   |    193.79
---------------------------------------------------------------
Unknown   |   2.36     |    5.108    |    1.96    |    9.428

我想在数据框列中找到最大值,并要向其中添加“未知”行值,并删除“未知行”。

我做了df ['A']。max(),df ['B']。max()和df ['C']。max(),它们返回列中的最大值,但是我该如何添加“行值未知吗?

我希望输出为:

Region    |     A      |       B     |      C     |    Total
===============================================================
Africa    |   100.10   |    20.135   |    10.02   |    130.255
---------------------------------------------------------------
Europe    |   202.71   |    50.102   |    18.31   |    271.122
---------------------------------------------------------------
India     |   30.98    |    155.288  |    12.63   |    198.898

说明:

(df ['A']。max()= 200.35)+ 2.36 = 202.71

(df ['B']。max()= 150.18)+ 5.108 = 155.288

(df ['C']。max()= 16.35)+ 1.96 = 18.31

1 个答案:

答案 0 :(得分:2)

mul与防毒面具一起使用

df=df.set_index('Region')
df+=(df==df.max()).mul(df.loc['Unknown'])  
df=df.drop('Unknown',axis=0)
df.Total=df.iloc[:,:-1].sum(1)
df
             A        B      C    Total
Region                                 
Africa   100.1   20.135  10.02  130.255
Europe  202.71   50.102  18.31  271.122
India    30.98  155.288  12.63  198.898