简体   繁体   English

根据其他列的值填充熊猫列的简便方法

[英]Easy way to fill up pandas column based on values of other columns

I have a pandas DataFrame with 3 columns like this: 我有3列这样的熊猫数据帧:

      a   b   c
0      1  1  10
1     2  12  10
2    16  13  10
3      9 16  10

In column c I would like to have the maximum of the 3 numbers in each row, so It should look like this: 在列c我想有最大的每一行中的3个数字,所以它应该是这样的:

      a   b   c
0      1  1  10
1     2  12  12
2    16  13  16
3      9 16  16

I could do this with for loops, but I was wondering if there is any simpler way to do this? 我可以使用for循环来做到这一点,但是我想知道是否有更简单的方法来做到这一点?

Use df.max(axis=1) 使用df.max(axis=1)

In [1807]: df['c'] = df.max(axis=1)

In [1808]: df
Out[1808]:
    a   b   c
0   1   1  10
1   2  12  12
2  16  13  16
3   9  16  16

Or, assign to return new dataframe 或者, assign返回新的数据框

In [1812]: df.assign(c=df.max(axis=1))
Out[1812]:
    a   b   c
0   1   1  10
1   2  12  12
2  16  13  16
3   9  16  16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM