0

I have two pandas dataframes, lets call them df1 and df2. df1 doesn't have df2's columns, I'm trying to set the values of df2 in df1 in a specific row, (df2 is generated when I loop over df1 so this is why I have the row index)

Basically trying to do something like this:

df1 = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]})
df2 = pd.DataFrame({"col3": [42], "col4": [83]})

row_index = 1

df1[df2.columns][row_index] = df2

Expected result is:

   col1  col2  col3   col4
0     1     3   NaN   NaN  
1     2     4   42    83

I tried all of the following and nothing is working:

df1 = pd.concat([df1, df2], axis=1)
df1 = pd.concat([df1.iloc[row_index], df2], axis=1)

df1[df2.columns] = df2
df1[df2.columns].iloc[row_index] = df2
ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • Youre [mre] should include a minimal example of the data -`df1` and `df2`, they can be *fake* data, I also see a `c` and `row_index` we know nothing about. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Feb 07 '21 at 14:29
  • How about `df1.join(df2, how='left')`, which is a left join on row indices to `df1`? – blacksite Feb 07 '21 at 14:30
  • I updated the question with an example. I'd rather not do joins because as mentioned, df2 is generated in a loop so I'd have to do hundreds of joins which is not optimal – ninesalt Feb 07 '21 at 14:36
  • Your whole approach sounds odd ("df2 is generated in a loop", "df2 is generated when I loop over df1"). You might want to describe the broader context of what you want to do, because the problem seems to lie somewhere else – mcsoini Feb 07 '21 at 20:17

1 Answers1

0

use loc to assign new values:

row_index = [1]
df1.loc[row_index, df2.columns] = df2.values

print(df1)

       col1  col2  col3  col4
    0     1     3   NaN   NaN
    1     2     4  42.0  83.0
Ferris
  • 5,325
  • 1
  • 14
  • 23