'Python de-aggregate / reverse-summarize / expand a dataset
Is there a way to de-aggregate / reverse-summarize / expand a dataset in python? For example, if I have a dataset in the format
year object count
2020 apple 3
2020 orange 1
2021 orange 2
2021 apple 4
I want it to look like:
year object
2020 apple
2020 apple
2020 apple
2020 orange
2021 orange
2021 orange
2021 apple
2021 apple
2021 apple
2021 apple
Is there an easy way to do this?
Solution 1:[1]
you can usepandas dataframe :
import pandas as pd
data = {'year':[2020,2020, 2021,2021],
'object':['apple','orange', 'orange','apple'],
'count':[3, 1, 2, 4]}
df = pd.DataFrame(data)
df = df[['year','object']].loc[df.index.repeat(df['count'])]
print(df)
output:
>>
year object
0 2020 apple
1 2020 apple
2 2020 apple
3 2020 orange
4 2021 orange
5 2021 orange
6 2021 apple
7 2021 apple
8 2021 apple
9 2021 apple
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
