1

I currently have n number of dataframes (dfs[n]) with IDs and X values and one dataframe (df) with IDs.

I would like to add X values to a new column in df if IDs of the X value match the ID of df and add 'X values n' as the column name.

for n in range(len(files)):
    if dfs[n]['ID'] == df['ID']:
        df = df.join(dfs[n]['X values'], on='ID', rsuffix = int(r))
    print(df)

I tried this code but doesn't work.

Thank you for your help!

3
  • Did you try combine first? Commented Oct 18, 2017 at 5:45
  • is there a reason why you are not simply iterating like for frame in dfs; df=df.join(frame...) Commented Oct 18, 2017 at 5:49
  • Not quick sure what you mean sorry, I'm pretty new with Python. But I did get an answer which works below. Thank though! Commented Oct 18, 2017 at 6:20

1 Answer 1

1

Setup

dfs = [pd.DataFrame({'ID': [1, 2, 3], 'X values': list('XYZ')}) for _ in range(4)]
df = pd.DataFrame(dict(ID=[1, 2, 3]))

Solution
You need to pd.concat after having pd.DataFrame.set_index

df.join(
    pd.concat(
        [d.set_index('ID')['X values'] for d in dfs],
        axis=1, keys=list(range(len(dfs)))
    ), on='ID')

   ID  0  1  2  3
0   1  X  X  X  X
1   2  Y  Y  Y  Y
2   3  Z  Z  Z  Z

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this helped me a lot !
I also wanted to put 'X values of n' as a name for each new columns. I tried changing your code a bit by putting keys = 'X values' + str(list(range(len(files))))) however it doesn't come out as a whole but only one letter per column. Do you think you have a solution for that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.