使用python在2个CSV文件中查找相等的值[重复]

a14dhokn  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(124)

此问题在此处已有答案

Pandas Merging 101(8个答案)
昨天就关门了。
我有2个excel文件,每个文件包含2列,如下所示:
文件1:

name     price
a        1
b        78
f        54
g        32
t        2

字符串
文件2:

name     price
f        4
x        23
e        76
a        4
o        0


我想读取file1并搜索file2 name等于file1 name的值并提取价格,然后将它们写入file2的价格列。
file2应如下所示:
文件2:

name     price
f        54
x        23
e        76
a        1
o        0


我已经尝试了如下(我已经保存的excel文件为CSV):

import pandas as pd
import numpy as np

df1  = pd.DataFrame(pd.read_csv('file1.csv'))
df2  = pd.DataFrame(pd.read_csv('file2.csv'))

if(df1[name] in df2[name]):
  df2[price] = df1[price]
  
df2.to_csv('file2.csv')

lpwwtiir

lpwwtiir1#

你需要先合并:

df2 = df2.merge(df1, on='name', how='left')

字符串
然后你可以使用np.,其中:

import numpy as np
df2['price'] = np.where(df2['price_y'].isnull(), df2['price_x'], df2['price_y'])


稍后,您可以删除合并的列:

df2.drop(columns=['price_x', 'price_y'], inplace=True)


这就是结果:

name     price
f        54
x        23
e        76
a        1
o        0

w1jd8yoj

w1jd8yoj2#

验证码

使用combine_first

df1.set_index('name').combine_first(df2.set_index('name'))\
   .reindex(df2['name']).reset_index()

字符串
看来提问者不知道Pandas的inplace,所以我将附上输出结果作为图像。


的数据
其他方式:使用concat & groupby + last

pd.concat([df2, df1]).groupby('name').last()\
  .reindex(df2['name']).reset_index()


相同结果
其他方式:merge & pop

df2.merge(df1, on='name', how='left', suffixes=['1', ''])\
   .assign(price=lambda x: x['price'].fillna(x.pop('price1')).astype('int'))


相同结果

示例代码

import pandas as pd
import io

file1 = '''name     price
a        1
b        78
f        54
g        32
t        2
'''

file2 = '''name     price
f        4
x        23
e        76
a        4
o        0
'''
df1 = pd.read_csv(io.StringIO(file1), sep='\s+')
df2 = pd.read_csv(io.StringIO(file2), sep='\s+')

相关问题