Here is a pandas DataFrame I would like to manipulate:
import pandas as pd
data = {"grouping": ["item1", "item1", "item1", "item2", "item2", "item2", "item2", ...],
"labels": ["A", "B", "C", "A", "B", "C", "D", ...],
"count": [5, 1, 8, 3, 731, 189, 9, ...]}
df = pd.DataFrame(data)
print(df)
>>> grouping labels count
0 item1 A 5
1 item1 B 1
2 item1 C 8
3 item2 A 3
4 item2 B 731
5 item2 C 189
6 item2 D 9
7 ... ... ....
I would like to "unfold" this dataframe into the following format:
grouping A B C D
item1 5 1 8 3
item2 3 731 189 9
.... ........
How would one do this? I would think that this would work:
pd.pivot_table(df,index=["grouping", "labels"]
but I get the following error:
DataError: No numeric types to aggregate
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…