I'm using Pandas to process the Dataframe, I need to use the Date
column to create additional columns:
I have filtered the Date
column to get 2022/2021 information only using:
df = df.loc[df["Date"].between("2022", "2021")]
Currently I have:
Date | Type | Initial | Number |
---|---|---|---|
2022 | Bin | S | 5 |
2022 | Bin | S | 6 |
2022 | Bin | S | 9 |
2021 | Bin | B | 5 |
2021 | Bin | B | 7 |
2021 | Bin | B | 0 |
I am currently trying to get the following output:
Type | 2022 | 2021 | Initial | Difference |
---|---|---|---|---|
Bin | 20 | 12 | S/B | 8 |
Following the process to create the bottom table, I have come across the following error
df = df.loc[df["Date"].between("2022", "2021")]
dfx = df.groupby(['Type ','Date ']).agg({'Initial ':lambda x: '/'.join([str(r) for r in x.unique()]),'Number':'sum'}).reset_index()
dfy = dfx.pivot(index=['Type ','Initial '], columns='Date ', values='Number').reset_index()
but I keep receiving the following error message:
raise KeyError(key) from err
KeyError: 'Date'
The KeyError: 'Date'
doesn't have a space in it. this is the only line I see where Data
doesn't have a trailing space.
df = df.loc[df["Date"].between("2022", "2021")]
Try adding a trailing space and see what happens:
df = df.loc[df["Date "].between("2022", "2021")]
Like @topsail says, trailing spaces in column names are a recipe for errors,
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.