简体   繁体   English

如何在 df 上组合行?

[英]How can I combine rows on a df?

I have this dataframe(after doing one hot encoding) how do I combine it together?我有这个数据帧(在进行一次热编码之后)我如何将它组合在一起?

    Code    Fund_Code_AADR  Fund_Code_AAXJ  Fund_Code_ABEQ  Fund_Code_ACSI  Fund_Code_ACVF  Fund_Code_ACWD  Fund_Code_ACWF  Fund_Code_ACWI  Fund_Code_ACWV  ... 
1625    MSFT    0   0   0   1   0   0   0   0   0   ... 
1635    MSFT    0   0   0   0   1   0   0   0   0   ... 
2067    MSFT    0   0   0   0   0   0   1   0   0   ... 
2423    MSFT    0   0   0   0   0   0   0   1   0   ... 
4517    IBM 0   0   0   0   0   0   0   0   1   ... 
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...

how do I combine all the values together?如何将所有值组合在一起? (ie one row for MFST and if it's 1 in any of those columns it's a 1 on the single row)? (即 MFST 的一行,如果它在任何这些列中为 1,则在单行上为 1)?

Update - sorry I wasn't clear there is multiple items in code field.更新 - 抱歉,我不清楚代码字段中有多个项目。 It's not all the same code but I want them merged as the same code这不是所有相同的代码,但我希望它们合并为相同的代码

This should work all codes are different这应该工作所有代码都不同

new_df=df.drop(['Code'],axis=1)
vals=new_df.any(axis=1).map({True:1,False:0})
df['any']=vals

but if there is any duplicates use group by但如果有任何重复使用 group by

new_df=df.groupby('Code').sum().any().map({True:1,False:0})
import numpy as np
df.replace(0, np.nan).ffill().bfill().iloc[:1,:]

or或者

df.groupby('Fund_Code_AADR').apply(lambda x: x.sum(0))

one solution would be to groupby sum and then convert all non zeroes to one:一种解决方案是 groupby sum 然后将所有非零转换为一:

df.groupby('code').sum().applymap(lambda x: 0 if x==0 else 1)

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

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