0

How to add multiple columns from one dataframe to another dataframe i laready figured out to add a single column but not getting multiple columns. I am a newbie

df

new['Symbol']= pd.Series(df['Symbol'])
dfnew['Symbol']['Desc']= pd.Series(df['Symbol']['Desc'])
anky
  • 74,114
  • 11
  • 41
  • 70
Harry
  • 31
  • 5
  • dfnew['Symbol']= pd.Series(df['Symbol']) # worked dfnew['Symbol']['Desc']= pd.Series(df['Symbol']['Desc']) # not working – Harry Jun 14 '19 at 08:13

1 Answers1

1

Use:

dfnew['Symbol'],dfnew['Desc']= df['Symbol'],df['Desc']

Or df.assign():

dfnew=dfnew.assign(Symbol=df.Symbol,Desc=df.Desc)

If needed initialize dfnew first as dfnew=pd.DataFrame()

anky
  • 74,114
  • 11
  • 41
  • 70
  • 1
    dfnew=dfnew.assign(Symbol=df.Symbol,Desc=df.Desc this worked for me thank you so much. – Harry Jun 14 '19 at 08:22
  • can you help with creating a file if exists add with number for Ex:SMA_Outliers.csv SMA_Outliers(1).csv ...... – Harry Jun 14 '19 at 09:25
  • @Hareesha that is a separate question which will require separate libraries, not pandas. :) I suggest you post a fresh question. – anky Jun 14 '19 at 09:26
  • @Hareesha may be you can check: https://stackoverflow.com/questions/17984809/how-do-i-create-a-incrementing-filename-in-python – anky Jun 14 '19 at 09:32