0

I have a dataframe that has a column 'mon/yr' that has month and year stored in this format Jun/19 , Jan/22,etc.

I want to Extract only these from that column - ['Jul/19','Oct/19','Jan/20','Apr/20','Jul/20','Oct/20','Jan/21','Apr/21','Jul/21','Oct/21','Jan/22'] and put them into a variable called 'dates' so that I can use it for plotting

My code which does not work -

dates = df["mon/yr"] == ['Jul/19','Oct/19','Jan/20','Apr/20','Jul/20','Oct/20','Jan/21','Apr/21','Jul/21','Oct/21','Jan/22']

This is a python code

Robert Long
  • 5,722
  • 5
  • 29
  • 50
Joshua
  • 5
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 12 '22 at 06:50
  • what do you want ?just filters single column or filtered rows? – Cherrymelon May 12 '22 at 07:34

2 Answers2

2

this is how to filter rows

df.loc[df['column_name'].isin(some_values)]
Cherrymelon
  • 412
  • 2
  • 7
  • 17
  • you should read this https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values – Cherrymelon May 12 '22 at 07:46
0

Using your dates list, if we wanted to extract just 'Jul/20' and 'Oct/20' we can do:

import pandas as pd

df = pd.DataFrame(['Jul/19','Oct/19','Jan/20','Apr/20','Jul/20','Oct/20','Jan/21','Apr/21','Jul/21','Oct/21','Jan/22'], columns = ['dates'])

mydates = ['Jul/20','Oct/20']

df.loc[df['dates'].isin(mydates)]

which produces:

    dates
4  Jul/20
5  Oct/20

So, for your actual use case, assuming that df is a pandas dataframe, and mon/yr is the name of the column, you can do:

dates = df.loc[df['mon/yr'].isin(['Jul/19','Oct/19','Jan/20','Apr/20','Jul/20','Oct/20','Jan/21','Apr/21','Jul/21','Oct/21','Jan/22'])]
Robert Long
  • 5,722
  • 5
  • 29
  • 50