[pandas] I have a question about changing the shape of the data frame.

Asked 6 days ago, Updated 6 days ago, 1 views

Be on

in the form of a time-series analysis for the following form for the dataframe I want to be changed. Help greatly appreciate it if you could.

Currently, the group value and date value are extracted from the above data to create a column, and then the value is put in the matching place, but the value is not changed. I'd really appreciate your advice.

pandas dataframe

2022-09-20 15:54

1 Answers

I'm not used to it either, so I can do pivot.

>>> df = pd.DataFrame({"group":list("AAABBB"), "date":["2021-06-09", "2021-06-08", "2021-06-07", "2021-06-09", "2021-06-08", "2021-06-07"], "num":[5,6,7,2,3,4]})
>>> df
  group        date  num
0     A  2021-06-09    5
1     A  2021-06-08    6
2     A  2021-06-07    7
3     B  2021-06-09    2
4     B  2021-06-08    3
5     B  2021-06-07    4
>>> df.pivot(index="group", columns="date", values="num")
date   2021-06-07  2021-06-08  2021-06-09
group                                    
A               7           6           5
B               4           3           2


2022-09-20 15:54

If you have any answers or tips


© 2022 pinfo. All rights reserved.