-1

I have a list of data-frames

liste = [df1, df2, df3, df4] 

sharing same index called "date". I concatenate this as follow:

pd.concat( (dd for dd in ll ), axis=1, join='inner')

But the columns have the same name. I can override the columns name manually, but I wonder if there is a way that the columns name will take the corresponding data-frame names, in this case "df1", "df2".

yatu
  • 86,083
  • 12
  • 84
  • 139
Crovish
  • 183
  • 10
  • @yatu... relax... and reinstate your answer. Based on your comments and communication Crovish will now understands how to appreciate help in any form. At least I do ;-) – ZF007 Jan 16 '20 at 11:04
  • Crovish... I've added a random lazyness implementation example for automaticly addeding column names. As you didn't provide a working example (read: your minimalistic code that produces an error and in pandas terms: example table with values) its hard to tailer the answers to the posted question. – ZF007 Jan 16 '20 at 11:06
  • @yatu.. from Crovish comment on my answer I understand that its not always obvious that we use sometimes "generic" terms or random values to debug presented code. That's okay but then we need to help them to get back on SO track. For example `conda create --name myenv` is a no-brainer once you understand it. Same as: `print('hello world')` or `setatrr(locations, 'foo', 'bar')`. But ... I understand your frustration too... (very recognizable if I'm too long on SO during one session ;-) – ZF007 Jan 16 '20 at 11:14
  • Crovish... you know what to do. Flag for removal of comments once you have read up the comments here and there. Don't forget to update Q with working example. Bots might otherwise decide and remove your question completely. And send the "five letters" to yatu in a `@yatu`. Then yatu or the other down-voter might remove the down-votes on your question ;-) – ZF007 Jan 16 '20 at 11:19

2 Answers2

1

You can replace them as followes:

import pandas as pd
from functools import reduce

liste = [df1, df2, df3, df4] 

df_final = reduce(lambda left,right: pd.merge(left,right,on='name'), liste)

Or:


... code snippet ...

df1.merge(df2,on='col_name').merge(df3,on='col_name').merge(df4,on='col_name')

Update based on comment:

An example for automated grabbing the column names of each you may integrate below code (while I assume its a single column array) to your liking:

colnames = {}

for i in range(len(dfs)):

    name = df[i].columns
    colnames[i+1] = name

... merge with code above ...
ZF007
  • 3,708
  • 8
  • 29
  • 48
  • You have to replace the example names `col_name` and `name` yourself by what you call it. If you provide a working example I could have used your column-names instead. You can add code to extract the labels of each df and add them automaticly ;-) – ZF007 Jan 16 '20 at 10:49
0

you could use merge

df=liste[0]
for data_frame in liste[1:]:
    df=df.merge(date_frame, left_index=True,right_index=True)

by default you'll get y_ appended to the columns so you'll end up with _y_y etc but you can control this with suffixes= so perhaps you use the position with an enumerate in the loop?

gdnaes
  • 158
  • 7