1

I want to select specific columns from multiple dataframes and combine them into one dataframe, how can I accomplish this?

df1

   count    grade
0   3        0
1   5        100
2   4        50.5
3   10       80.10


df2

    books   saving
0   4        10
1   5        9000
2   8        70
3   10       500

How can I select the saving column from df2 and combine with grade column from df1 to form a separate pandas dataframe that looks like the below.

    grade     saving
0   0           10
1   100        9000
2   50.5        70
3   80.10       500


I tried

df = pd.DataFrame([df1['grade'],df2['saving']])
print(df)

but the outcome is not what I wanted.

nerd
  • 473
  • 5
  • 15
  • 1
    `df = pd.DataFrame({ 'grade':df1['grade'], 'saving':df2['saving'] })` or `pd.concat([df1['grade'], df2['saving']], axis=1)` – SomeDude Jul 08 '22 at 02:16

1 Answers1

3
df = pd.concat([df1['grade'], df2['saving']], axis=1)

Similar question has been answered here.

Pandas documentation for this function: pandas.concat

Minh Dao
  • 827
  • 8
  • 21