gpt4 book ai didi

python - 在 Python Pandas 中合并两个数据集

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:53 25 4
gpt4 key购买 nike

我有两个以下格式的数据集,希望根据城市+年龄+性别将它们合并为一个数据集。提前致谢

数据集1:

        City    Age  Gender            Source         Count
0 California 15-24 Female Amazon Prime Video 14629
1 California 15-24 Female Fubo TV 3840
2 California 15-24 Female Hulu 54067
3 California 15-24 Female Netflix 11713
4 California 15-24 Female Sling TV 10642

数据集2:

         City    Age  Gender           Source     Feeds
0 California 15-24 Female Blogs 150
1 California 15-24 Female Customsite 57
2 California 15-24 Female Discussions 28
3 California 15-24 Female Facebook Comment 555
4 California 15-24 Female Google+ 19

预期结果数据集:

    City      Age   Gender            Source          Count
California 15-24 Female Amazon Prime Video 14629
California 15-24 Female Fubo TV 3840
California 15-24 Female Hulu 54067
California 15-24 Female Netflix 11713
California 15-24 Female Sling TV 10642
California 15-24 Female Blogs 150
California 15-24 Female Customsite 57
California 15-24 Female Discussions 28
California 15-24 Female Facebook Comment 555
California 15-24 Female Google+ 19

注:Feeds/Count 含义相同。因此可以将它们中的任何一个作为合并数据集中的列名称。

最佳答案

使用pandas.concat使用重命名列来对齐列 - 需要在两个DataFrames中使用相同的列:

df = pd.concat([df1, df2.rename(columns={'Feeds':'Count'})], ignore_index=True)
print (df)
City Age Gender Source Count
0 California 15-24 Female Amazon Prime Video 14629
1 California 15-24 Female Fubo TV 3840
2 California 15-24 Female Hulu 54067
3 California 15-24 Female Netflix 11713
4 California 15-24 Female Sling TV 10642
5 California 15-24 Female Blogs 150
6 California 15-24 Female Customsite 57
7 California 15-24 Female Discussions 28
8 California 15-24 Female Facebook Comment 555
9 California 15-24 Female Google+ 19

替代DataFrame.append - 不纯python append :

df = df1.append(df2.rename(columns={'Feeds':'Count'}), ignore_index=True)
print (df)
City Age Gender Source Count
0 California 15-24 Female Amazon Prime Video 14629
1 California 15-24 Female Fubo TV 3840
2 California 15-24 Female Hulu 54067
3 California 15-24 Female Netflix 11713
4 California 15-24 Female Sling TV 10642
5 California 15-24 Female Blogs 150
6 California 15-24 Female Customsite 57
7 California 15-24 Female Discussions 28
8 California 15-24 Female Facebook Comment 555
9 California 15-24 Female Google+ 19

关于python - 在 Python Pandas 中合并两个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030450/

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