简体   繁体   English

是否有 pandas 方法通过特定列值为每一行添加 dataframe

[英]Is there a pandas way to add a dataframe for each row by a specific column value

I have a dataframe here:我这里有一个 dataframe:

    name       role
0   Allen      Director
1   Kendrick   Food
2   Sean       Webmaster
3   Jacob      PR

I also have another dataframe:我还有一个 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?有没有一种 pandas 方法可以将 dataframe 的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'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.我尝试通过遍历行来完成,但我的 dataframe 比我上面提供的示例大很多,我正在寻找 pandas 方法来执行此操作。

One option is to assign df2.power to df1 as a list, then explode it:一种选择是assign df2.power作为列表分配给df1 ,然后将其explode

out = df1.assign(power=[df2['power'].tolist()]*len(df1)).explode('power').reset_index(drop=True)

If you have pandas >= 1.2.0., you can cross-merge:如果你有 pandas >= 1.2.0.,你可以交叉合并:

out = df1.merge(df2, how='cross')

Output: Output:

        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
6       Sean  Webmaster    eat
7       Sean  Webmaster  sleep
8       Sean  Webmaster   code
9      Jacob         PR    eat
10     Jacob         PR  sleep
11     Jacob         PR   code

暂无
暂无

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

相关问题 有没有办法向pandas数据框添加新列,将新列的每个唯一值附加到数据帧的每个现有行? - Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe? 将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值 - Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column 为 pandas 数据框中的每一行添加特定的竞争 - add specific competition to each row in pandas dataframe 获取熊猫数据框中特定行和列的值 - get a value of a specific row and column in pandas dataframe 在 dataframe 中添加行,其值与特定列中的值相同 - Add row in dataframe with same value as in specific column 有没有办法根据 pandas 中的特定条件在 dataframe 的所有行中添加列名? - Is there any way to add column name in all the row of a dataframe based on a specific condition in pandas? 遍历 pandas dataframe 的每一行后获取特定列 - Get specific column after iterating over each row of pandas dataframe 如果特定列的一个值为 NaN,有没有办法使用 ffill 替换整个 pandas dataframe 行? - Is there a way to replace a whole pandas dataframe row using ffill, if one value of a specific column is NaN? 通过对每一行进行操作,在数据框中创建列的“pandas”方法是什么? - What is the `pandas` way to create a column in a dataframe by operating on each row? 如何获取pandas数据帧的每一行中特定值的频率 - How to get the frequency of a specific value in each row of pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM