基于多相似列合并Pandas DataFrame:补充2022年7-12月数据
合并两个DataFrame生成指定结构的df3
需求说明
需要将df1与df2合并:df1包含2022年1-6月的数据,df2包含2022年7-12月的数据,基于name和year列匹配,把df2的7-12月数据补充到df1中,生成指定结构的df3。
原始数据
df1数据
id r1 r2 r3 r4 r5 month name year 1 4 4 4 4 4 1 nets 2021 2 20 20 20 20 20 2 nets 2021 3 8 8 8 8 8 3 nets 2021 4 5 5 5 5 5 4 nets 2021 5 3 3 3 3 3 5 nets 2021 6 19 19 19 19 19 6 nets 2021 6 19 19 19 19 19 7 nets 2021 6 19 19 19 19 19 8 nets 2021 6 19 19 19 19 19 9 nets 2021 6 1 1 1 1 1 10 nets 2021 6 11 11 11 11 11 11 nets 2021 6 19 19 19 19 19 12 nets 2021 7 5 5 5 5 5 1 nets 2022 8 9 9 9 9 9 2 nets 2022 9 12 12 12 12 12 3 nets 2022 10 21 21 21 21 21 4 nets 2022 11 2 2 2 2 2 5 nets 2022 12 17 17 17 17 17 6 nets 2022 13 3 3 3 3 3 1 warriors 2021 14 7 7 7 7 7 2 warriors 2021 15 3 3 3 3 3 3 warriors 2021 16 4 4 4 4 4 4 warriors 2021 17 1 1 1 1 1 5 warriors 2021 18 4 4 4 4 4 6 warriors 2021 19 1 1 1 1 1 7 warriors 2021 20 7 7 7 7 7 8 warriors 2021 21 18 18 18 18 18 9 warriors 2021 22 12 12 12 12 12 10 warriors 2021 23 7 7 7 7 7 11 warriors 2021 24 4 4 4 4 4 12 warriors 2021 25 11 11 11 11 11 1 warriors 2022 26 6 6 6 6 6 2 warriors 2022 27 2 2 2 2 2 3 warriors 2022 28 17 17 17 17 17 4 warriors 2022 29 24 24 24 24 24 5 warriors 2022 30 22 22 22 22 22 6 warriors 2022
df2数据
id month name 1 2 3 4 5 year 1 7 nets 10 25 14 4 22 2022 2 8 nets 5 8 6 24 18 2022 3 9 nets 18 8 8 14 16 2022 4 10 nets 19 11 13 8 9 2022 5 11 nets 16 25 3 9 6 2022 6 12 nets 17 11 18 3 24 2022 7 7 warriors 15 24 11 2 25 2022 8 8 warriors 1 7 18 9 17 2022 9 9 warriors 11 11 8 18 20 2022 10 10 warriors 16 20 3 24 2 2022 11 11 warriors 10 24 6 4 19 2022 12 12 warriors 8 16 12 19 22 2022
解决方案
步骤说明
- 调整df2列名:将df2中列名
1,2,3,4,5重命名为r1,r2,r3,r4,r5,与df1列名对齐。 - 筛选有效列:保留df2中
r1,r2,r3,r4,r5,month,name,year列,去除无关的id列。 - 合并数据:将处理后的df2追加到df1末尾。
- 排序与重置索引:按
name、year、month排序,重置索引并将索引命名为idx(从1开始)。
代码实现
import pandas as pd # 加载数据(实际场景可通过pd.read_csv等方式读取) # df1 = pd.read_csv('df1.csv', sep='\s+') # df2 = pd.read_csv('df2.csv', sep='\s+') # 重命名df2的列 df2_renamed = df2.rename(columns={'1': 'r1', '2': 'r2', '3': 'r3', '4': 'r4', '5': 'r5'}) # 筛选需要的列 df2_processed = df2_renamed[['r1', 'r2', 'r3', 'r4', 'r5', 'month', 'name', 'year']] # 合并两个DataFrame df_combined = pd.concat([df1, df2_processed], ignore_index=True) # 排序并重置索引 df3 = df_combined.sort_values(by=['name', 'year', 'month'], ignore_index=True) df3.index = df3.index + 1 # 让索引从1开始 df3.index.name = 'idx' # 输出结果 print(df3)
目标df3结果
idx r1 r2 r3 r4 r5 month name year 1 4 4 4 4 4 1 nets 2021 2 20 20 20 20 20 2 nets 2021 3 8 8 8 8 8 3 nets 2021 4 5 5 5 5 5 4 nets 2021 5 3 3 3 3 3 5 nets 2021 6 19 19 19 19 19 6 nets 2021 6 19 19 19 19 19 7 nets 2021 6 19 19 19 19 19 8 nets 2021 6 19 19 19 19 19 9 nets 2021 6 1 1 1 1 1 10 nets 2021 6 11 11 11 11 11 11 nets 2021 6 19 19 19 19 19 12 nets 2021 7 5 5 5 5 5 1 nets 2022 8 9 9 9 9 9 2 nets 2022 9 12 12 12 12 12 3 nets 2022 10 21 21 21 21 21 4 nets 2022 11 2 2 2 2 2 5 nets 2022 12 17 17 17 17 17 6 nets 2022 13 10 25 14 4 22 7 nets 2022 14 5 8 6 24 18 8 nets 2022 15 18 8 8 14 16 9 nets 2022 16 19 11 13 8 9 10 nets 2022 17 16 25 3 9 6 11 nets 2022 18 17 11 18 3 24 12 nets 2022 19 3 3 3 3 3 1 warriors 2021 20 7 7 7 7 7 2 warriors 2021 21 3 3 3 3 3 3 warriors 2021 22 4 4 4 4 4 4 warriors 2021 23 1 1 1 1 1 5 warriors 2021 24 4 4 4 4 4 6 warriors 2021 25 1 1 1 1 1 7 warriors 2021 26 7 7 7 7 7 8 warriors 2021 27 18 18 18 18 18 9 warriors 2021 28 12 12 12 12 12 10 warriors 2021 29 7 7 7 7 7 11 warriors 2021 30 4 4 4 4 4 12 warriors 2021 31 11 11 11 11 11 1 warriors 2022 32 6 6 6 6 6 2 warriors 2022 33 2 2 2 2 2 3 warriors 2022 34 17 17 17 17 17 4 warriors 2022 35 24 24 24 24 24 5 warriors 2022 36 22 22 22 22 22 6 warriors 2022 37 15 24 11 2 25 7 warriors 2022 38 1 7 18 9 17 8 warriors 2022 39 11 11 8 18 20 9 warriors 2022 40 16 20 3 24 2 10 warriors 2022 41 10 24 6 4 19 11 warriors 2022 42 8 16 12 19 22 12 warriors 2022
内容的提问来源于stack exchange,提问作者user2100039
相关产品推荐
相关产品推荐

