我有以下数据框
Q A
A a h
A b i
A c j
B d k
B a l
B b m
C c n
我想得到假和groupby
a b c d e f g
A h i j nan nan nan nan
B l nan nan nan k nan nan
C nan nan n nan nan nan nan
col=df.Q
我必须申请get_dummies
和groupby
。但我无法理解。
我怎样才能得到这个结果?
答案 0 :(得分:4)
您似乎需要reset_index
与pivot
:
df = df.reset_index().pivot(index='index', columns='Q', values='A')
print (df)
Q a b c d
index
A h i j None
B l m None k
C None None n None
然后,如果有必要reindex_axis
和replace
:
cols = list('abcdefg')
print (df.reindex_axis(cols, axis=1).replace({None:np.nan}))
Q a b c d e f g
index
A h i j NaN NaN NaN NaN
B l m NaN k NaN NaN NaN
C NaN NaN n NaN NaN NaN NaN
编辑:
如果groupby
join
print (df)
Q A
A a h
A b i
A c j
B d k
B a l
B b m <-duplicates B b
B b t <-duplicates B b
C c n
df = df.reset_index().groupby(['index','Q'])['A'].apply(','.join).unstack()
print (df)
Q a b c d
index
A h i j None
B l m,t None k
C None None n None
与#aggfunc='first' - get only first value, another values are lost
df1 = df.reset_index().pivot_table(index='index', columns='Q', values='A', aggfunc='first')
print (df1)
Q a b c d
index
A h i j None
B l m None k
C None None n None
Q a b c d
#aggfunc='sum' - summed data, no separator
df2 = df.reset_index().pivot_table(index='index', columns='Q', values='A', aggfunc='sum')
print (df2)
index
A h i j None
B l mt None k
C None None n None
Q a b c d
#aggfunc=','.join - summed data with separator
df3 = df.reset_index().pivot_table(index='index', columns='Q', values='A', aggfunc=','.join)
print (df3)
index
A h i j None
B l m,t None k
C None None n None
的重复数据更好:
@OneToMany(mappedBy="cliente")
pivot_table
的另一种可能解决方案:
class MyClass : ... {
QGraphicsScene m_scene;
QGraphicsPixmapItem m_pixmapItem; // must be after m_scene: in C++ this order matters!
...
}
MyClass::MyClass() {
...
m_scene->addItem(&m_pixmapItem);
}
void MyClass::myMethod() {
m_pixmapItem.setPixmap(QPixmap::fromImage(LifeGrid));
}