gpt4 book ai didi

python - Pandas 将性别列拆分为两列

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:05 27 4
gpt4 key购买 nike

我有下表: https://ibb.co/DMGCgD2

我想在每个邮政编码中将(属性类型)拆分为两列 count_Single_Family 和 count_Multi_Family:

我使用了这段代码,但我不知道下一步该做什么:

b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()

我可以用 sql 做到这一点,但我一直试图让它与 pandas 一起工作,但没有成功。感谢您的帮助。编辑*输入:

    import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input

期望的输出:

    out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002], 
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],

})
out

最佳答案

pd.crosstab + merge

Pandas 有一种基于索引和值进行交叉制表的方法。然后,您只需将这些结果与您的原始数据框合并。

df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

res = df.merge(df_cross, left_on='Zip Code', right_index=True)

print(res)

Zip Code Total Males Total Female Property Type Multi-Family \
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1

Single Family
0 1
1 1
2 1
3 2
4 2
5 2

关于python - Pandas 将性别列拆分为两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53471647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com