Home > front end >  Return a Cell from a Pandas Dataframe based on other values
Return a Cell from a Pandas Dataframe based on other values

Time:05-20

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.

CodePudding user response:

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

CodePudding user response:

Another method is doing this.

df[df['Year']==2021].iloc[0]['Total']

This part df[df['Year']==2021] creates a new dataframe, where we only have values from 2021, and the .iloc fetches the value at position 0 in the 'Total' column

CodePudding user response:

Would simple filter suffice?

df[(df.Year == 2021) & (df.Month == 1)].Total
  • Related