熊猫:比较列表值并编写新列

时间:2016-07-15 09:22:04

标签: python pandas

我对my old post提出了一个新问题。在那篇文章中,问题被简化了,只有两个可供比较。现在,假设我有超过2,例如3,频率(1,1,0)。我想查看此列表,如果最大值出现多次,则写入0,否则,写入最大值的列标签,如上一篇文章中所述。我试图调整那篇文章的第一个答案,但我被卡住了。

任何帮助将不胜感激,谢谢你的关注。 : - )

编辑:

我真正的df的一个例子是:

|FID |geometry|GridCode|catarro|constipado|gripa|gripe|resfriado|resfrio 0 |9592|... |9592 |1 |0 |0 |3 | 3 | 1

在这种情况下,所需的输出将是: |FID |geometry|GridCode|catarro|constipado|gripa|gripe|resfriado|resfrio| max 0 |9592|... |9592 |1 |0 |0 |3 | 3 | 1 | 0

1 个答案:

答案 0 :(得分:3)

编辑:

# You should drop all extra fields
# don't worry they are still present in original dataframe (df)
words = df.drop(['FID'], axis=1)

# Get maximums for each row
maxes = words.max(axis=1)

# Create new column with the features names with maximum values
df['max'] = words.idxmax(axis=1)

# Create a mask with non-accepted rows
mask = (
    words.values.ravel() == maxes.values.repeat(len(words.columns)).ravel()
).reshape(-1,len(words.columns)).astype(int).sum(axis=1)>1

# Wipe 'max' column in non-accepted rows
df.ix[mask,'max'] = 0
相关问题