Home>

Always I am indebted.

Regarding the title, I am currently studying pivot_table for pandas and am unable to solve the following problems.

Corresponding dataframe

Arrival date Product code Product name Quantity
0 November 16 11111 Pencil 100
1 November 16 11112 Colored pencils 50
2 November 17 11112 Colored pencils 130
3 November 18 11111 Pencil 200
4 November 18 11112 Colored pencils 30
5 November 18 11113 Magic 500
6 November 18 11114 Brush 10
7 November 19th 11113 Magic 150
8 November 19th 11114 Brush 30
9 November 20 11111 Pencil 50


In response to the above, I am trying to execute the following code, pivot_table.


import pandas as pd

data_csv = "./data.csv"
data_pvt = pd.read_csv (data_csv, encoding = "utf_8_sig")
data_pvt.pivot_table (index = "arrival date", values ​​= "quantity", aggfunc = "sum")

print (data_pvt)


The purpose is to calculate the total quantity for each arrival date.
However, when I run the code, I don't see any change in the original dataframe.

There may be rudimentary mistakes.
Sorry for your inconvenience, but could you please tell me where there is a mistake?

  • Answer # 1

    pivot_tableReturns a newly generated DataFrame. The original DataFrame remains unchanged.

    data_csv = "./data.csv"
    df = pd.read_csv (data_csv, encoding = "utf_8_sig")
    df_pvt = df.pivot_table (index = "arrival date", values ​​= "quantity", aggfunc = "sum")
    print (df_pvt)