I have the following dataset in a Pandas Dataframe:
| Id | Year | Month | Total |
|---|---|---|---|
| 0 | 2020 | 9 | 11788.33 |
| 1 | 2020 | 10 | 18373.99 |
| 2 | 2020 | 11 | 31018.59 |
| 3 | 2020 | 12 | 29279.30 |
| 4 | 2021 | 1 | 1875.10 |
| 5 | 2021 | 2 | 9550.06 |
| 6 | 2021 | 3 | 33844.39 |
| 7 | 2021 | 4 | 33126.53 |
| 8 | 2021 | 5 | 12910.05 |
| 9 | 2021 | 6 | 44628.63 |
| 10 | 2021 | 7 | 25830.03 |
| 11 | 2021 | 8 | 54463.08 |
| 12 | 2021 | 9 | 49723.93 |
| 13 | 2021 | 10 | 23753.81 |
| 14 | 2021 | 11 | 52532.49 |
| 15 | 2021 | 12 | 7467.32 |
| 16 | 2022 | 1 | 24333.54 |
| 17 | 2022 | 2 | 12394.11 |
| 18 | 2022 | 3 | 76575.46 |
| 19 | 2022 | 4 | 95119.82 |
| 20 | 2022 | 5 | 63048.05 |
I am trying to dynamically return the value from the Total column based on the first month (Month 1) from last year (Year 2021). Solution is 1875.10.
I am using Python in PyCharm to complete this.
Note: The "Id" column is the one that is automatically generated when using a pandas Dataframe. I believe it is called an index within Pandas.
Any help would be greatly appreciated.
Thank you.
>Solution :
You can use .loc[]:
df.loc[(df['Year'] == 2021) & (df['Month'] == 1), 'Total']
Which will give you:
0 1875.1
Name: Total, dtype: float64
To get the actual number you can add .iloc[] on the end:
df.loc[(df['Year'] == 2021) & (df['Month'] == 1), 'Total'].iloc[0]
Output:
1875.1