I'd like to apply the pd.pivot_table() to get the number of each categorical value for column 'categories'.
Here, the basic info of the dataset is as following:
df.info()
Data columns (total 3 columns):
location 2270 non-null object
time 2270 non-null object
categories 2270 non-null object
dtypes: object(3)
My code:
table=pd.pivot_table(df,values=['categories'],
index=['location','time'],
columns=['categories'],
aggfunc='count',fill_value=0)
table.head()
Expected result is:
location time Cat1 Cat2
L1 Jan-2020 5 1
L1 Feb-2020 2 1
L2 Jan-2019 4 3
L2 Feb-2020 5 0
But my result is:
location time
L1 Jan-2020
L1 Feb-2020
L2 Jan-2019
L2 Feb-2020
I tried to create the simple dataframe by input the data as the example below, then I could get my expected result. But if I import my whole dataframe from csv file, it failed.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html
Thanks for all the suggestions and help in advance.