我想重新格式化一个数据帧,以便显示两列组合的计数.这是一个示例数据帧:
my_df = pd.DataFrame({'a': ['first', 'second', 'first', 'first', 'third', 'first'],
'b': ['foo', 'foo', 'bar', 'bar', 'baz', 'baz'],
'c': ['do', 're', 'mi', 'do', 're', 'mi'],
'e': ['this', 'this', 'that', 'this', 'those', 'this']})
看起来像这样:
a b c e
0 first foo do this
1 second foo re this
2 first bar mi that
3 first bar do this
4 third baz re those
5 first baz mi this
我希望它创建一个新的数据框,计算列a和c之间的组合,如下所示:
c do mi re
a
first 2.0 2.0 NaN
second NaN NaN 1.0
third NaN NaN 1.0
如果我将values参数设置为等于其他列,我可以使用pivot_table执