我有一个Excel文件,其中包含这样的数据:
Name Job
Damian Engineer
Rose Musician
Eric Dancer
我想将此文本与具有相同作业行的文本文件合并:
25, Engineer
26, Dancer
所以最终的输出是:
Name Job Age
Damian Engineer 25
Eric Dancer 26
我已经写了一些开始,但是我不确定如何获得最终输出
xls = pd.read_excel(excel_file)
excel_dict = xls.astype(str).to_dict('list')
with open('hello.txt', 'rb') as f1:
csv_reader = csv.reader(f1)
for row in csv_reader:
job = row[1]
if job in excel_dict['Job']:
excel_dict['Age'] = row[0]
能否请您帮我实现上述最终输出?我正在使用熊猫来读取excel文件,并使用csv来读取文本文件。
答案 0 :(得分:1)
尝试一下:
In [1411]: excel_df = pd.read_excel('myexcel.xlsx')
In [1412]: excel_df
Out[1412]:
Name Job
0 Damian Engineer
1 Rose Musician
2 Eric Dancer
In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
In [1418]: txt_df.columns = ['Age', 'Job']
In [1419]: txt_df
Out[1419]:
Age Job
0 25 Engineer
1 26 Dancer
In [1447]: pd.merge(excel_df, txt_df, on='Job')
Name Job Age
0 Damian Engineer 25
1 Eric Dancer 26
答案 1 :(得分:0)
pandas具有merge
功能。假设您的csv有标头,否则您可以提供标头或使用left_on
和right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
答案 2 :(得分:0)
这将为您工作。您必须删除“作业”列或从各列之间的txt文件中删除空格。
import pandas as pd
xls = pd.read_excel('sample2.xlsx')
csv_df = pd.read_csv('sample.txt', header=None)
csv_df.columns = ['Age', 'Job']
csv_df['Job'] = csv_df['Job'].map(str.strip)
xls = xls.merge(csv_df, on='Job')
print(xls)