简体   繁体   English

append 或将一个 dataframe 中的值连接到另一个 Z6A8064B5DF4794555500553C47C55057DZ 中的 Z251D2CEBBFE303B95E4AZDC1 中的每一行

[英]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.我有一台 dataframe,里面只有一排。 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.我想 append 从我的第一个 dataframe 的一列到我第二个的每一行的值。

df1: df1:

id ID Value价值
1 1 word单词

df2: df2:

id ID data数据
1 1 a一个
2 2 b b
3 3 c c

Output I'm seeking: Output 我正在寻找:

df2 df2

id ID data数据 Value价值
1 1 a一个 word单词
2 2 b b word单词
3 3 c c word单词

I figured that this was along the right lines, but it listed out NaN for all rows:我认为这是正确的,但它列出了所有行的 NaN:

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.我想我可以加入 id 值,然后将该值复制到所有行,但我认为有一种更简洁的方法可以做到这一点。

Thanks in advance for any help you can provide!提前感谢您提供的任何帮助!

Just get the first element in the value column of df1 and assign it to value column of df2只需获取 df1 的 value 列中的第一个元素并将其分配给 df2 的 value 列

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM