我有下表:
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来做到这一点,但我一直试图使其与熊猫一起工作,但没有运气。感谢您的帮助。
编辑*
输入:

    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

熊猫提供了一种基于索引和值进行交叉制表的方法。然后,您只需要将这些结果与原始数据框合并即可。

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/

10-12 18:19