赋值方式Pandas.加入()

2024-05-19 01:34:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下df

df
      Site        Process     parameter       value    unit   
0      Mid  Biomass plant        cap-up     5000.00      
1      Mid  Biomass plant      inv-cost   875000.00    
2      Mid  Biomass plant  depreciation       25.00      
3      Mid     Coal plant        cap-up        0.00      
4      Mid     Coal plant      inv-cost   600000.00    
5      Mid     Coal plant  depreciation       40.00    

我想要一个unit的df,如下所示:

df_unit
      unit
0       MW    
1    €/MWh  
2        a

我想把dfdf_unit合并在一起。可能是用熊猫做这个的最好方法,但我不知道怎么用。那么你能帮我从dfdf_unit得到下面的df_merged吗?你知道吗

以下输出是预期结果。你知道吗

df_merged
      Site        Process     parameter       value    unit   
0      Mid  Biomass plant        cap-up     5000.00      MW
1      Mid  Biomass plant      inv-cost   875000.00   €/MWh
2      Mid  Biomass plant  depreciation       25.00       a
3      Mid     Coal plant        cap-up        0.00      MW
4      Mid     Coal plant      inv-cost   600000.00   €/MWh
5      Mid     Coal plant  depreciation       40.00       a

Tags: dfsiteunitprocessmwcapupcost
1条回答
网友
1楼 · 发布于 2024-05-19 01:34:30

在您的评论中,您提到要将其指定给正确的参数。所以,在units数据框中需要一个参数列。你知道吗

假设你有一个

步骤1:-使用字典并读取其中的单元数据帧。你知道吗

dictitems = dict(zip(df_Unit[Parameter_Col], df_Unit[Unit_Col]))

上面的代码所做的是,它希望在units数据框中的参数列和单位列之间创建一个项目zip。你知道吗

步骤2:-现在调用数据帧内的字典

df["Unit_Column"] = df["parameter"].map(dictitems).fillna("Unit Not Found")

上面的代码所做的是,将字典映射到实际数据帧上的参数列,并将其添加为新列。你知道吗

如果你对此有任何疑问,请告诉我。你知道吗

相关问题 更多 >

    热门问题