Python 使用透视表重塑熊猫中的数据帧
我有一个如下所示的数据帧:Python 使用透视表重塑熊猫中的数据帧,python,pandas,Python,Pandas,我有一个如下所示的数据帧: baz qux one A one B two C three A one B one C one two three A C A B B C 我正试图把它改造成这样: baz qux one A one B two C three A one B one C one two three A C A B B
baz qux
one A
one B
two C
three A
one B
one C
one two three
A C A
B
B
C
我正试图把它改造成这样:
baz qux
one A
one B
two C
three A
one B
one C
one two three
A C A
B
B
C
我很困惑这是否可能,如果可能,你会怎么做。我曾尝试使用
pivot\u table
方法作为pd.pivot\u table(cols='baz',rows='qux')
,但这引发了一个类型错误。我想我是个白痴,在这里错过了一些基本的东西。有什么想法吗?我不确定这是否是最好的方法,但它确实起到了作用:
import io
import pandas as pd
data = u'baz,qux\none,A\none,B\ntwo,C\nthree,A\none,B\none,C'
df = pd.read_csv(io.StringIO(data))
new = pd.DataFrame()
for key, group in df.groupby('baz'):
new = pd.concat([new, pd.DataFrame(group.reset_index().qux, columns=[key])],
axis=1)
print new.replace(np.nan, '')
这给了我们:
one two three
0 A C A
1 B
2 B
3 C
使用透视表,您可以得到一个矩阵,显示哪个baz
对应哪个qux
:
>>> df['foo'] = 1 # Add aggregation column
>>> df.pivot_table('foo', cols='baz', rows=['qux'])
one three two
A 1 1 NaN
B 1 NaN NaN
C 1 NaN 1
这不完全是你所要求的,但也许足够了:
import numpy as np
import pandas as pd
df = pd.DataFrame({'baz':'one one two three one one'.split(),
'qux': list('ABCABC')})
grouped = df.groupby(['baz', 'qux'])
df2 = grouped.apply(pd.DataFrame.reset_index, drop=True)['qux'].unstack(level=0)
df2.reset_index(drop=True, inplace=True)
df2 = df2.reindex(columns='one two three'.split())
df2 = df2.replace(np.nan, '')
print(df2)
屈服
one two three
0 A A
1 B
2 B
3 C C