R dcast 在 python pandas 中的等效项

2024-04-05

我正在尝试在 python 中执行与以下命令等效的操作:

test <- data.frame(convert_me=c('Convert1','Convert2','Convert3'),
                   values=rnorm(3,45, 12), age_col=c('23','33','44'))
test

library(reshape2)
t <- dcast(test, values ~ convert_me+age_col, length  )
t

也就是说,这个:

convert_me   values     age_col
Convert1     21.71502      23
Convert2     58.35506      33
Convert3     60.41639      44

变成这样:

values     Convert2_33 Convert1_23 Convert3_44
21.71502          0           1           0
58.35506          1           0           0
60.41639          0           0           1

我知道使用虚拟变量我可以获取列的值并将其转换为列的名称,但是有没有办法像 R 那样轻松地合并它们(组合)?


您可以使用crosstab http://pandas.pydata.org/pandas-docs/stable/generated/pandas.crosstab.html为此功能:

In [14]: pd.crosstab(index=df['values'], columns=[df['convert_me'], df['age_col']])
Out[14]: 
convert_me  Convert1  Convert2  Convert3
age_col           23        33        44
values                                  
21.71502           1         0         0
58.35506           0         1         0
60.41639           0         0         1

or the pivot_table http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pivot_table.html (with len作为聚合函数,但这里你必须fillna手动带零的 NaN):

In [18]: df.pivot_table(index=['values'], columns=['age_col', 'convert_me'], aggfunc=len).fillna(0)
Out[18]: 
age_col           23        33        44
convert_me  Convert1  Convert2  Convert3
values                                  
21.71502           1         0         0
58.35506           0         1         0
60.41639           0         0         1

请参阅此处有关此内容的文档:http://pandas.pydata.org/pandas-docs/stable/reshaping.html#pivot-tables-and-cross-tabulations http://pandas.pydata.org/pandas-docs/stable/reshaping.html#pivot-tables-and-cross-tabulations

pandas 中的大多数函数将返回多级(分层)索引,在本例中为列。如果你想像 R 一样将其“融入”一个级别,你可以这样做:

In [15]: df_cross = pd.crosstab(index=df['values'], columns=[df['convert_me'], df['age_col']])

In [16]: df_cross.columns = ["{0}_{1}".format(l1, l2) for l1, l2 in df_cross.columns]

In [17]: df_cross
Out[17]: 
          Convert1_23  Convert2_33  Convert3_44
values                                         
21.71502            1            0            0
58.35506            0            1            0
60.41639            0            0            1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R dcast 在 python pandas 中的等效项 的相关文章

随机推荐