如果匹配的列合并两个csv文件,然后覆盖

时间:2018-10-23 07:53:12

标签: python pandas csv

示例我有这两个csv,如何覆盖a.csv中的列type的值,或者如果它与a.csv和b.csv中的列fruit中的字符串都匹配,则如何替换< / p>

a.csv

fruit,name,type
apple,anna,A
banana,lisa,A
orange,red,A
pine,tin,A

b.csv

fruit,type
banana,B
apple,B

如何输出此内容或如何覆盖

fruit,name,type
apple,anna,B
banana,lisa,B
orange,red,A
pine,tin,A

我正在尝试用熊猫做这个,但我不知道接下来会发生什么

df1=pd.read_csv("sha1_vsdt.csv",delimiter=",",error_bad_lines=False,engine = 'python',quoting=3)
df2=pd.read_csv("final.csv",delimiter=",",error_bad_lines=False,engine = 'python',quoting=3)

df = pd.merge(df1, df2, on='SHA-1', how='outer')

4 个答案:

答案 0 :(得分:1)

根据您输入的信息

import pandas as pd
df1=pd.read_csv("a.csv")
df2=pd.read_csv("b.csv")
df = pd.merge(df1, df2, on='fruit', how='outer')
df['type_x'] = df['type_y'].combine_first(df['type_x'])
del df["type_y"]
df = df[pd.notnull(df['name'])]

输入df1

    fruit   name    type
0   apple   anna    A
1   banana  lisa    A
2   orange  red     A
3   pine    tin     A

输入df2

    fruit   type
0   banana  B
1   lemon   B

输出

   fruit    name    type_x
0   apple   anna    A
1   banana  lisa    B
2   orange  red     A
3   pine    tin     A

如果您使用不同的文件使用不同的列名

import pandas as pd
df1=pd.read_csv("a.csv")
df2=pd.read_csv("b.csv")
df = pd.merge(df1, df2, on='fruit', how='outer')
df[df.columns[2]] = df[df.columns[3]].combine_first(df[df.columns[2]])
del df[df.columns[3]]
df = df[pd.notnull(df[df.columns[1]])]

答案 1 :(得分:0)

使用map创建的Series的{​​{3}},然后用set_index用原始列值重写丢失的不匹配值:

#if possible duplicated fruit column
s = df2.drop_duplicates('fruit').set_index('fruit')['type']
df1['type'] = df1['fruit'].map(s).fillna(df1['type'])
print (df1)
    fruit  name type
0   apple  anna    B
1  banana  lisa    B
2  orange   red    A
3    pine   tin    A

答案 2 :(得分:0)

您不需要let filteredNames = myDict.compactMap { $0.value < 5 ? $0.key : nil } ,可以通过简单的merge来实现:

.loc

答案 3 :(得分:0)

您可以先对齐索引update,然后再对齐reset_index

# align indices
df1 = pd.read_csv(s1).set_index('fruit')
df2 = pd.read_csv(s2).set_index('fruit')

# update
df1.update(df2)

# reset index
res = df1.reset_index()

print(res)

    fruit  name type
0   apple  anna    B
1  banana  lisa    B
2  orange   red    A
3    pine   tin    A

设置

from io import StringIO

s1 = StringIO("""fruit,name,type
apple,anna,A
banana,lisa,A
orange,red,A
pine,tin,A""")

s2 = StringIO("""fruit,type
banana,B
apple,B""")