16

I have been searching online and found similar questions but still couldn't find the answer to what I'm looking for. I have 2 excel files:

data1

ColumnA columnB columnC columnD A B C D A B C D A B C D 

data2

ColumnE columnF columnG E F G E F G E F G 

I want to add the column F from data2 to data1:

ColumnA columnB columnC columnD columnF A B C D F A B C D F A B C D F 

I tried

data2['columnF'] = data1['columnF'] #doesn't work 

also tried

data1['columnF'] = '' #adding a columnF to data1 merg_left = pd.merge(left=data1,right=data2, how='left', left_on='columnF', right_on='columnF') #gave me a weird output file 
1
  • 1
    You need to add more detail. Do they have the same index? if so then data2['columnF'] = data1['columnF'] would work. Are the same shape? Do they have some common index? pd.concat is probably what you want to use but until we know what the indices and shape are we can't tell you exactly how to use it. Commented Nov 4, 2015 at 21:28

2 Answers 2

18
import pandas as pd f_column = data2["columnF"] data1 = pd.concat([data1,f_column], axis = 1) data1 
 columnA columnB columnC columnF 0 a b c f 1 a b c f 2 a b c f 
Sign up to request clarification or add additional context in comments.

Comments

15
import pandas as pd import io data = """ ColumnA columnB columnC columnD A B C D A B C D A B C D """ data1 = """ ColumnE columnF columnG E F G E F G E F G """ df = pd.read_csv(io.StringIO(data), delimiter='\s+') df1 = pd.read_csv(io.StringIO(data1), delimiter='\s+') df['columnF'] = pd.Series(df1['columnF']) print(df) 

Will give you:

 ColumnA columnB columnC columnD columnF 0 A B C D F 1 A B C D F 2 A B C D F 

4 Comments

thank you! i think i understand your code but the data and data1 are 2 files that i have read in using pd.read_csv and pd.read_excel, instead of the simple tables i put up there (wanted to make everything look clean here), and when i assign io.StringIO(data) and io.StringIO(data1) the error message gave "initial_value must be str or None, not DataFrame" , do you know how i should fix this? thanks
The io.StringIO() are just a way to create your sample data. You should use read_csv and read_excel as you have. The main part is df['columnF'] = pd.Series(df1['columnF'])
thanks that worked. if i want to insert that 'columnF' from df1 to the column after 'columnA' in df, would i do something like df['columnA'+1] = df['columnF'] ?
That won't work. You'll need insert for that. df.insert(1,'columnF',df1['columnF'])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.