对于每一行,返回最小值的列名称 - pandas

时间:2017-08-29 19:44:15

标签: python pandas dataframe

我想找到这个问题的[ { "ID": "c21j8nzzy3151vv06m54yyd1u", "Version": { "Index": 67824 }, "CreatedAt": "2017-02-21T05:42:31.467777741Z", "UpdatedAt": "2017-08-29T19:48:35.4252027Z", "Spec": { "Role": "worker", "Availability": "active" }, "Description": { "Hostname": "swarm-worker-1", "Platform": { "Architecture": "x86_64", "OS": "linux" }, "Resources": { "NanoCPUs": 2000000000, "MemoryBytes": 7305609216 }, "Engine": { "EngineVersion": "1.12.2", "Plugins": [ { "Type": "Network", "Name": "bridge" }, { "Type": "Network", "Name": "host" }, { "Type": "Network", "Name": "null" }, { "Type": "Network", "Name": "overlay" }, { "Type": "Volume", "Name": "local" } ] } }, "Status": { "State": "unknown", "Message": "Node moved to \"unknown\" state due to leadership change in cluster" } } ] 等价物。

For each row return the column name of the largest value

我想在下面的数据框中添加一个新列,这是每行中最低值的列名。

pandas

获得min很简单: Multi-Use Charging Performer Controls Value for Money All Rounder 0 1.569541 0.290916 2.396734 0.881500 3.171563 1.950175 1 0.906542 2.296172 0.162809 1.604936 0.730633 0.532835 2 0.442924 0.970764 1.264364 0.295140 2.034826 0.824529 3 0.167663 1.367973 0.877306 0.683562 1.653964 0.444136 4 0.870290 0.547844 1.703054 0.209975 2.476787 1.260371

如何根据min返回列名?

1 个答案:

答案 0 :(得分:6)

你可以做到

df['lowest_col'] = df.idxmin(axis=1)

你得到了

    Multi-Use   Charging    Performer   Controls    Value for Money All Rounder lowest_col
0   1.569541    0.290916    2.396734    0.881500    3.171563    1.950175    Charging
1   0.906542    2.296172    0.162809    1.604936    0.730633    0.532835    Performer
2   0.442924    0.970764    1.264364    0.295140    2.034826    0.824529    Controls
3   0.167663    1.367973    0.877306    0.683562    1.653964    0.444136    Multi-Use
4   0.870290    0.547844    1.703054    0.209975    2.476787    1.260371    Controls