0

I'm trying to find the average value of two columns 'GBP' and 'USD' based on the specific year of 2020 from the 'Date' column inside a Pandas DataFrame.

The original question: "What was the average value of the £ in 2020 against the $"

What I've done:

import pandas as pd

df = pd.read_csv ('C:/Users/user/AppData/Local/Programs/Python/Python39/Scripts/usd_vs_gbp_euro_2011_2021.csv')

print(df.groupby('Date').GBP.mean())
print(df.groupby('Date').USD.mean())

However, this code prints the mean for every year, not just the year 2020. Can anyone point out where I'm getting wrong or suggest some solutions?

Note: I'm new to Python and using DataFrames.

Parts of the DataFrame

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Angelism
  • 23
  • 5
  • This is a straightforward application of a filter. Filters are covered in any PANDAS tutorial. Where are you stuck on that part? – Prune May 19 '21 at 22:14
  • Please include a _small_ subset of your data as a __copyable__ piece of code that can be used for testing as well as your expected output for the __provided__ data. See [MRE - Minimal, Reproducible, Example](https://stackoverflow.com/help/minimal-reproducible-example), and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker May 19 '21 at 22:27

1 Answers1

0

Assuming that the data-type of your Date column is string, this is how you do it:

df_2020 = df[df['Date'].str.contains('2020')]
USD_mean = df_2020['USD'].mean()
GBP_mean = df_2020['GBP'].mean()
pakpe
  • 5,391
  • 2
  • 8
  • 23