我试图将多个csv合并为一个,它给了我这样的重复 这是我试图合并的csv的例子:
blink.csv
account_id kills deaths assists...(a couple more columns)
1015 4 4 2
1015 3 5 5
大多数csv都是这样的,里面有不同的数据。 这是合并的结果(不是我想要的)
account_id_x, hero_id_x, kills_x, deaths_x, assists_x,
0 1015,4565,5121,4687 15,71,106,87 2.5 7 3.75
0 1015,4565,5121,4687 15,71,106,87 2.5 7 3.75
0 1015,4565,5121,4687 15,71,106,87 2.5 7 3.75
0 1015,4565,5121,4687 15,71,106,87 2.5 7 3.75
1 1015,4565,5121,4687 73,86,93,30 2.75 4.5 2.75
1 1015,4565,5121,4687 73,86,93,30 2.75 4.5 2.75
1 1015,4565,5121,4687 73,86,93,30 2.75 4.5 2.75
1 1015,4565,5121,4687 73,86,93,30 2.75 4.5 2.75
它将数据保留为4的副本,但是account_id基本上都是重复的,所以如何将csv合并在一起而没有4个重复的行,但保留account_id而不将其视为所有重复项? 还有一种方法可以删除列标题末尾的_x和_y吗?
这是我尝试使用的python代码(但是给我的结果是我不想要的):
blink = pd.read_csv('blink.csv',keep_default_na=False,na_values=[""])
faithbian = pd.read_csv('faithbian.csv',keep_default_na=False,na_values=[""])
iceice = pd.read_csv('iceice.csv',keep_default_na=False,na_values=[""])
shadow = pd.read_csv('Shadow.csv',keep_default_na=False,na_values=[""])
y = pd.read_csv('y.csv',keep_default_na=False,na_values=[""])
df = pd.concat([blink,faithbian,iceice,shadow,y],axis=0).drop_duplicates()
a_df = pd.concat([df.groupby(['match_id'])['account_id'].apply(lambda x: "%s" % ', '.join(x.astype(str))),
df.groupby(['match_id'])['hero_id'].apply(lambda x: "%s" % ', '.join(x.astype(str))),
df.groupby(['match_id'])['kills','deaths','assists','xp_per_min','gold_per_min','hero_damage','hero_healing','tower_damage'].mean()],axis=1).drop_duplicates()
con_df = pd.merge(a_df, df, left_index=True, right_on='match_id').drop_duplicates()
con_df.to_csv('out.csv')