是否有熊猫通过特定的列值添加每行数据框
我在这里有一个数据框架:
name role
0 Allen Director
1 Kendrick Food
2 Sean Webmaster
3 Jacob PR
我还有另一个数据框架:
power
0 eat
1 sleep
2 code
是否有一种熊猫的方法可以向团队数据框中的每个成员添加power
dataframe,以使其看起来像这样?
name role power
0 Allen Director eat
1 Allen Director sleep
2 Allen Director code
3 Kendrick Food eat
4 Kendrick Food sleep
5 Kendrick Food code
...
我已经尝试通过迭代行迭代来做,但是我的数据框比上面提供的示例要大得多,我正在寻找一种熊猫的方法来执行此操作。
I have a dataframe here:
name role
0 Allen Director
1 Kendrick Food
2 Sean Webmaster
3 Jacob PR
I also have another dataframe:
power
0 eat
1 sleep
2 code
Is there a pandas way to add the power
dataframe to each member in the team dataframe to make it look like this?
name role power
0 Allen Director eat
1 Allen Director sleep
2 Allen Director code
3 Kendrick Food eat
4 Kendrick Food sleep
5 Kendrick Food code
...
I've tried doing by iterating through the rows but my dataframe is a lot larger than the example I have provided above and I am looking for a pandas approach to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是
分配
df2.power
todf1
作为列表,然后爆炸
IT:如果您有pandas > = 1.2.0。,您可以交叉合并:
输出:
One option is to
assign
df2.power
todf1
as a list, thenexplode
it:If you have pandas >= 1.2.0., you can cross-merge:
Output: