merging a excel file and a text file with similar job
up vote
0
down vote
favorite
I have an excel file that holds a data like this:
Name Job
Damian Engineer
Rose Musician
Eric Dancer
I want to merge this with a textfile with rows with the same job:
25, Engineer
26, Dancer
So the final out put would be:
Name Job Age
Damian Engineer 25
Eric Dancer 26
I have written a something to start but I'm not sure how to have the final output
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]
Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.
python pandas csv
add a comment |
up vote
0
down vote
favorite
I have an excel file that holds a data like this:
Name Job
Damian Engineer
Rose Musician
Eric Dancer
I want to merge this with a textfile with rows with the same job:
25, Engineer
26, Dancer
So the final out put would be:
Name Job Age
Damian Engineer 25
Eric Dancer 26
I have written a something to start but I'm not sure how to have the final output
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]
Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.
python pandas csv
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an excel file that holds a data like this:
Name Job
Damian Engineer
Rose Musician
Eric Dancer
I want to merge this with a textfile with rows with the same job:
25, Engineer
26, Dancer
So the final out put would be:
Name Job Age
Damian Engineer 25
Eric Dancer 26
I have written a something to start but I'm not sure how to have the final output
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]
Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.
python pandas csv
I have an excel file that holds a data like this:
Name Job
Damian Engineer
Rose Musician
Eric Dancer
I want to merge this with a textfile with rows with the same job:
25, Engineer
26, Dancer
So the final out put would be:
Name Job Age
Damian Engineer 25
Eric Dancer 26
I have written a something to start but I'm not sure how to have the final output
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]
Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.
python pandas csv
python pandas csv
asked Nov 8 at 10:52
LearningNoob
195
195
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Try this:
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
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from yourhello.txt
before the names and after comma:25,Engineer
26,Dancer
Then createtxt_df
and do merge again.
– Mayank Porwal
Nov 8 at 11:23
add a comment |
up vote
0
down vote
pandas has a merge
function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on
and right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
add a comment |
up vote
0
down vote
This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.
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)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try this:
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
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from yourhello.txt
before the names and after comma:25,Engineer
26,Dancer
Then createtxt_df
and do merge again.
– Mayank Porwal
Nov 8 at 11:23
add a comment |
up vote
0
down vote
accepted
Try this:
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
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from yourhello.txt
before the names and after comma:25,Engineer
26,Dancer
Then createtxt_df
and do merge again.
– Mayank Porwal
Nov 8 at 11:23
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try this:
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
Try this:
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
edited Nov 8 at 11:24
answered Nov 8 at 10:59
Mayank Porwal
2,3291619
2,3291619
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from yourhello.txt
before the names and after comma:25,Engineer
26,Dancer
Then createtxt_df
and do merge again.
– Mayank Porwal
Nov 8 at 11:23
add a comment |
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from yourhello.txt
before the names and after comma:25,Engineer
26,Dancer
Then createtxt_df
and do merge again.
– Mayank Porwal
Nov 8 at 11:23
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Hi, I tried this but it is returning an empty dataframe?
– LearningNoob
Nov 8 at 11:16
Remove spaces from your
hello.txt
before the names and after comma: 25,Engineer
26,Dancer
Then create txt_df
and do merge again.– Mayank Porwal
Nov 8 at 11:23
Remove spaces from your
hello.txt
before the names and after comma: 25,Engineer
26,Dancer
Then create txt_df
and do merge again.– Mayank Porwal
Nov 8 at 11:23
add a comment |
up vote
0
down vote
pandas has a merge
function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on
and right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
add a comment |
up vote
0
down vote
pandas has a merge
function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on
and right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
add a comment |
up vote
0
down vote
up vote
0
down vote
pandas has a merge
function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on
and right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
pandas has a merge
function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on
and right_on
xls = pd.read_excel(excel_file)
other_file = pd.read_csv('hello.txt', sep=',')
xls = xls.merge(other_file, on='Job')
answered Nov 8 at 11:00
Alex
649520
649520
add a comment |
add a comment |
up vote
0
down vote
This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.
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)
add a comment |
up vote
0
down vote
This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.
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)
add a comment |
up vote
0
down vote
up vote
0
down vote
This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.
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)
This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.
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)
answered Nov 8 at 11:32
L. Letovanec
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206240%2fmerging-a-excel-file-and-a-text-file-with-similar-job%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown