简体   繁体   English

基于df.columns和Series.index名称将pandas.Dataframe与pandas.Series合并的最佳方法是什么?

[英]what is the best way to merge pandas.Dataframe with pandas.Series based on df.columns and Series.index names?

im facing the following problem and i dont know what is the cleanest/smartest way to solve it. 我面临以下问题,我不知道什么是最干净/最聪明的解决方案。 I have a dataframe called wfm that contains the input for my simulation 我有一个名为wfm的数据框,其中包含模拟输入

        wfm.head()
Out[51]: 
   OPN  Vin  Vout_ref  Pout_ref  CFN  ...  Cdclink      Cdm         L     k    ron
0    6  350       750     80500    1  ...  0.00012  0.00012  0.000131 -0.37  0.001
1    7  400       800     92000    1  ...  0.00012  0.00012  0.000131 -0.37  0.001
2    8  350       900     80500    1  ...  0.00012  0.00012  0.000131 -0.37  0.001
3    9  450       750    103500    1  ...  0.00012  0.00012  0.000131 -0.37  0.001
4   10  450       900    103500    1  ...  0.00012  0.00012  0.000131 -0.37  0.001

[5 rows x 13 columns]

then every simulation loop I receive 2 Series outputs_rms and outputs_avg that look like this: 然后,每个模拟循环我都会收到2个如下所示的系列output_rms和output_avg:

outputs_rms                             outputs_avg 
Out[53]:                                Out[54]:    
time.rms                0.057751        time.avg    5.78E-02
Vi_dc.voltage.rms       400             Vi_dc.voltage.avg   4.00E+02
Vi_dc.current.rms       438.333188      Vi_dc.current.avg   3.81E+02
Vi_dc.power.rms         175333.2753     Vi_dc.power.avg 1.53E+05
Am_in.current.rms       438.333188      Am_in.current.avg   3.81E+02
Cdm.voltage.rms         396.614536      Cdm.voltage.avg 3.96E+02
Cdm.current.rms         0.213185        Cdm.current.avg -5.14E-05
motor_phU.current.rms   566.035833      motor_phU.current.avg   -5.67E+02
motor_phU.voltage.rms   296.466083      motor_phU.voltage.avg   -9.17E-02
motor_phV.current.rms   0.061024        motor_phV.current.avg   2.58E-02
motor_phV.voltage.rms   1.059341        motor_phV.voltage.avg   -1.24E-09
motor_phW.current.rms   566.005071      motor_phW.current.avg   5.67E+02
motor_phW.voltage.rms   297.343876      motor_phW.voltage.avg   9.17E-02
S_ULS.voltage.rms       305.017804      S_ULS.voltage.avg   2.65E+02
S_ULS.current.rms       358.031053      S_ULS.current.avg   -1.86E+02
S_UHS.voltage.rms       253.340047      S_UHS.voltage.avg   1.32E+02
S_UHS.current.rms       438.417985      S_UHS.current.avg   3.81E+02
S_VLS.voltage.rms       295.509073      S_VLS.voltage.avg   2.64E+02
S_VLS.current.rms       0               S_VLS.current.avg   0.00E+00
S_VHS.voltage.rms       152.727975      S_VHS.voltage.avg   1.32E+02
S_VHS.current.rms       0.061024        S_VHS.current.avg   -2.58E-02
S_WLS.voltage.rms       509.388666      S_WLS.voltage.avg   2.64E+02
S_WLS.current.rms       438.417985      S_WLS.current.avg   3.81E+02
S_WHS.voltage.rms       619.258959      S_WHS.voltage.avg   5.37E+02
S_WHS.current.rms       357.982417      S_WHS.current.avg   -1.86E+02
Cdclink.voltage.rms     801.958092      Cdclink.voltage.avg 8.02E+02
Cdclink.current.rms     103.73088       Cdclink.current.avg 2.08E-05
Am_out.current.rms      317.863371      Am_out.current.avg  1.86E+02
Vo_dc.voltage.rms       800             Vo_dc.voltage.avg   8.00E+02
Vo_dc.current.rms       317.863371      Vo_dc.current.avg   -1.86E+02
Vo_dc.power.rms         254290.6969     Vo_dc.power.avg -1.49E+05
CFN                     1               CFN 1.00E+00
OPN                     6               OPN 6.00E+00
dtype:                  float64         dtype:  float64

then my goal is to place outputs_rms and outputs_avg on the right line of wfm, based on 'CFN' and 'OPN' values. 那么我的目标是基于'CFN'和'OPN'值将output_rms和output_avg放在wfm的右行。

what is your suggestions? 您有什么建议? thanks Riccardo 谢谢里卡多

Suppose that you create these series as outputs output_rms_1, output_rms_2, etc., than the series can be combined in one dataframe 假设您将这些系列创建为输出output_rms_1,output_rms_2等,然后将该系列组合到一个数据帧中

import pandas as pd
dfRms = pd.DataFrame([output_rms_1, output_rms_2, output_rms_3])

Next output, say output_rms_10, can simply be added by using: 下一个输出,例如output_rms_10,可以使用以下命令简单地添加:

dfRms = dfRms.append(output_rms_10, ignore_index=True)

Finally, when all outputs are joined into one Dataframe, you can merge the original wfm with the output, ie 最后,当所有输出都合并到一个Dataframe中时,可以将原始wfm与输出合并,即

result = pd.merge(wfm, dfRms, on=['CFN', 'OPN'], how='left')

Similarly for avg. 同样的平均值。

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

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