简体   繁体   English

根据来自另一个熊猫数据框的列在熊猫数据框中创建新行

[英]Create new rows in a Pandas Dataframe based on a column from another pandas dataframe

I have a dataframe DF1 which looks like this:我有一个数据框 DF1,它看起来像这样:

Account Name帐户名称 Task Type任务类型 Flag旗帜 Cost成本
Account 1账户 1 Repair修理 True真的 $100 100 美元
Account 2账户 2 Repair修理 True真的 $200 200 美元
Account 3账户 3 Repair修理 False错误的 $300 300 美元

DF2 looks like this: DF2 看起来像这样:

Country国家 Percentage百分比
US我们 30% 30%
Canada加拿大 20% 20%
India印度 50% 50%

I want to create DF3 based on DF1 & DF2 by doing the following:我想通过执行以下操作基于 DF1 和 DF2 创建 DF3:

  1. Filter rows with where the Flag = True过滤标志 = True 的行
  2. Create a new column 'Calculated_Cost' which will multiply the 'Cost' column in DF1 with percentage column of DF2 & create multiple rows based on the number of rows in DF2创建一个新列“Calculated_Cost”,它将 DF1 中的“Cost”列与 DF2 的百分比列相乘,并根据 DF2 中的行数创建多行

The Final output would look like this:最终输出如下所示:

Account Name帐户名称 Task Type任务类型 Flag旗帜 Cost成本 Country国家 Calculated_Cost计算成本
Account 1账户 1 Repair修理 True真的 $100 100 美元 US我们 $30 30 美元
Account 1账户 1 Repair修理 True真的 $100 100 美元 Canada加拿大 $20 20 美元
Account 1账户 1 Repair修理 True真的 $100 100 美元 India印度 $50 50 美元
Account 2账户 2 Repair修理 True真的 $200 200 美元 US我们 $60 60 美元
Account 2账户 2 Repair修理 True真的 $200 200 美元 Canada加拿大 $40 40 美元
Account 2账户 2 Repair修理 True真的 $200 200 美元 India印度 $100 100 美元
Account 3账户 3 Repair修理 False错误的 $300 300 美元 Nan Nan

Use:利用:

df1['Cost'] = df1['Cost'].str.lstrip('$').astype(int)
df2['Percentage'] = df2['Percentage'].str.rstrip('%').astype(int).div(100)

df = pd.concat([df1[df1['Flag']].merge(df2, how='cross'), df1[~df1['Flag']]])
df['Calculated_Cost'] = df['Cost'].mul(df.pop('Percentage'))
print (df)
  Account Name Task Type   Flag  Cost Country  Calculated_Cost
0    Account 1    Repair   True   100      US             30.0
1    Account 1    Repair   True   100  Canada             20.0
2    Account 1    Repair   True   100   India             50.0
3    Account 2    Repair   True   200      US             60.0
4    Account 2    Repair   True   200  Canada             40.0
5    Account 2    Repair   True   200   India            100.0
2    Account 3    Repair  False   300     NaN              NaN

I am sure there is a more efficient way to do this... but I got it done using the following code:我确信有一种更有效的方法可以做到这一点......但我使用以下代码完成了它:

import pandas as pd

df1 = pd.DataFrame(
    {
     'Account Name': ['Account 1', 'Account 2', 'Account 3'],
     'Task Type': ['Repair', 'Repair', 'Repair'],
     'Flag': ['True', 'True', 'False'],
     'Cost': ['$100', '$200', '$300']
       }
    )

df2 = pd.DataFrame(
    {
     'Country': ['US', 'Canada', 'India'],
     'Percentage': ['30%', '20%', '50%']
       }
    )

df1['Cost'] = df1['Cost'].str.lstrip('$').astype(int)
df2['Percentage'] = df2['Percentage'].str.rstrip('%').astype(int).div(100)
filtered_df_true = df1.loc[df1['Flag'] == 'True'] 
filtered_df_false = df1.loc[df1['Flag'] == 'False']
df3 = filtered_df_true.assign(key=1).merge(df2.assign(key=1), how = 'outer', on='key')
df3['Calculated Cost'] = df3['Cost']*df3['Percentage']
frames = [df3, filtered_df_false]
result = pd.concat(frames)
result.pop('key')
result.pop('Percentage')
print(result)

暂无
暂无

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

相关问题 Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? pandas:通过将 DataFrame 行与另一个 DataFrame 的列进行比较来创建新列 - pandas: Create new column by comparing DataFrame rows with columns of another DataFrame 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column 如何根据 Pandas 数据框中的其他行创建新列? - How create a new column based on other rows in pandas dataframe? 如何基于一组行在Pandas DataFrame中新建列 - How to create a new column in Pandas DataFrame based on a group of rows 熊猫:在一个数据框中创建新列,并根据与另一个数据框中的匹配键进行匹配 - Pandas: create new column in one dataframe with values based on matching key from another dataframe 根据日期,使用来自另一个 dataframe 的值在 pandas dataframe 中创建一个新列 - Create a new column in pandas dataframe with values from another dataframe, based on date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM