Home > Blockchain >  How can i take specific Months out from a Column in python
How can i take specific Months out from a Column in python

Time:05-12

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

CodePudding user response:

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'])]

CodePudding user response:

this is how to filter rows

df.loc[df['column_name'].isin(some_values)]
  • Related