I've got a dataframe like this:
import pandas as pd
data = {
'POS': ['1','2','1','3','4'],
'TYPE': ['A','A','A','B','C'],
'VOLUME': [34,2,12,200,1],
}
df = pd.DataFrame(data)
df
Table:
POS TYPE VOLUME
0 1 A 34
1 2 A 2
2 1 A 12
3 3 B 200
4 4 C 1
Task:
I want to automatically create new columns for each different value in column TYPE
and get the number of appearances of each value grouped by POS
(assume that there are a lot of different values, not only A, B and C). Additionally I simply want to sum VOLUME
.
The result should look like this:
|--------------|--------------|--------------|--------------|--------------|
| POS | Amount_A | Amount_B | Amount_C | Sum_Volume |
|--------------|--------------|--------------|--------------|--------------|
| 1 | 2 | 0 | 0 | 46 |
| 2 | 1 | 0 | 0 | 2 |
| 3 | 0 | 1 | 0 | 200 |
| 4 | 0 | 0 | 1 | 1 |
|--------------|--------------|--------------|--------------|--------------|
Attempt:
I know how to do it for VOLUME
: df.groupby(['POS'])['VOLUME'].sum()
. But I dont't konw how to manage getting new columns without something like "If TYPE
== 'A' then ...".
Try this:
import pandas as pd
data = {
'POS': ['1','2','1','3','4'],
'TYPE': ['A','A','A','B','C'],
'VOLUME': [34,2,12,200,1],
}
df = pd.DataFrame(data)
df = pd.concat([df,pd.get_dummies(df["TYPE"])],axis=1)
print(df.groupby("POS").sum())
OUTPUT:
VOLUME ABC POS 1 46 2 0 0 2 2 1 0 0 3 200 0 1 0 4 1 0 0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.