创建一个列变量,以变量的均值为条件,以其他两个变量为条件

时间:2022-05-17 14:19:08

I have a data frame that shows the mean 'dwdime' for each of the given conditions:

我有一个数据框,显示每个给定条件的平均'dwdime':

DIMExCand_means = DIMExCand.groupby(['cycle', 'coded_state', 'party.orig', 'comtype']).mean()

创建一个列变量,以变量的均值为条件,以其他两个变量为条件

I have created a pivot table from DIMExCand_means with the following command and output:

我已经使用以下命令和输出从DIMExCand_means创建了一个数据透视表:

DIMExCand_master = pd.pivot_table(DIMExCand_means,index=["Cycle","State"])

创建一个列变量,以变量的均值为条件,以其他两个变量为条件

However, some data gets lost in the process. I would like to add columns to the 'DIMExCand_master' dataframe that includes the mean 'dwdime' score given each possible combination of 'party.orig' and 'comptype', as this will allow me to have one entry per 'cycle'-'coded_state'.

但是,有些数据会在此过程中丢失。我想在'DIMExCand_master'数据框中添加列,其中包括'party.orig'和'comptype'的每个可能组合的平均'dwdime'得分,因为这将允许我每个'周期' - '有一个条目coded_state”。

2 个解决方案

#1


1  

Let's try:

DIMExCand_means = DIMExCand_means.reset_index()
DIMExCand_master = DIMExCand_master.reset_index()

pd.merge(DIMExCand_means, DIMExCand_master, left_on=['cycle','coded_state'], right_on=['Cycle','State'])

#2


0  

Thanks!

I ended up going with:

我结束了:

DIMExCand_dime = pd.pivot_table(DIMExCand, values = 'dwdime', index ["Cycle","State"], columns='ID', aggfunc=np.mean)

DIMExCand_dime = pd.pivot_table(DIMExCand,values ='dwdime',index [“Cycle”,“State”],columns ='ID',aggfunc = np.mean)

#1


1  

Let's try:

DIMExCand_means = DIMExCand_means.reset_index()
DIMExCand_master = DIMExCand_master.reset_index()

pd.merge(DIMExCand_means, DIMExCand_master, left_on=['cycle','coded_state'], right_on=['Cycle','State'])

#2


0  

Thanks!

I ended up going with:

我结束了:

DIMExCand_dime = pd.pivot_table(DIMExCand, values = 'dwdime', index ["Cycle","State"], columns='ID', aggfunc=np.mean)

DIMExCand_dime = pd.pivot_table(DIMExCand,values ='dwdime',index [“Cycle”,“State”],columns ='ID',aggfunc = np.mean)