Simnicjon
Simnicjon

Reputation: 105

append or join value from one dataframe to every row in another dataframe in Pandas

I'm normally OK on the joining and appending front, but this one has got me stumped.

I've got one dataframe with only one row in it. I have another with multiple rows. I want to append the value from one of the columns of my first dataframe to every row of my second.

df1:

id Value
1 word

df2:

id data
1 a
2 b
3 c

Output I'm seeking:

df2

id data Value
1 a word
2 b word
3 c word

I figured that this was along the right lines, but it listed out NaN for all rows:

df2 = df2.append(df1[df1['Value'] == 1])

I guess I could just join on the id value and then copy the value to all rows, but I assumed there was a cleaner way to do this.

Thanks in advance for any help you can provide!

Upvotes: 0

Views: 2007

Answers (1)

Shubham Periwal
Shubham Periwal

Reputation: 2248

Just get the first element in the value column of df1 and assign it to value column of df2

df2['value'] = df1.loc[0, 'value']

Upvotes: 1

Related Questions