How to efficiently extract date year from dataframe header in Pandas?

balandongiv

The objective is to extract df under the month-year category while omitting other. The code below one way how this objective can be achieved

df = DataFrame ( [['PP1', 'LN', 'T1', 'C11', 'C21', 'C31', 'C32']] )
df.columns =['dummy1','dummy2', 'Jan-20', 'Feb-20', 'Jan 2021', 'Feb 2080','Dec 1993']
extract_header_name=list(df.columns.values)
lookup_list= ['Jan', 'Feb', 'Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
month_year_list=[i for e in lookup_list for i in extract_header_name if e in i]

Output

['Jan-20', 'Jan 2021', 'Feb-20', 'Feb 2080', 'Dec 1993']

However, I wonder if is another efficient or pandas built module to achieve similar result?

jezrael

Use str.contains with values joined by | for regex or - it means Jan or Feb... and filter by boolean indexing with df.columns:

month_year_list = df.columns[df.columns.str.contains('|'.join(lookup_list))].tolist()
print (month_year_list)
['Jan-20', 'Feb-20', 'Jan 2021', 'Feb 2080', 'Dec 1993']

Or use Series.str.startswith with convert list to tuple:

month_year_list = df.columns[df.columns.str.startswith(tuple(lookup_list))].tolist()

Another idea if only this 2 formats of datetimes:

s = df.columns.to_series()
s1 = pd.to_datetime(s, format='%b-%y', errors='coerce')
s2 = pd.to_datetime(s, format='%b %Y', errors='coerce')
month_year_list = df.columns[s1.fillna(s2).notna()].tolist()
print (month_year_list)
['Jan-20', 'Feb-20', 'Jan 2021', 'Feb 2080', 'Dec 1993']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to extract only year from the date in dataframes?

From Java

Extract year from date

From Dev

How to extract date , month and year from the date we pass to javascript?

From Java

python pandas extract year from datetime --- df['year'] = df['date'].year is not working

From Dev

python pandas extract year from datetime: df['year'] = df['date'].year is not working

From Dev

python pandas extract year from datetime: df['year'] = df['date'].year is not working

From Dev

How to extract day, month, and year from date returned by QML Calender?

From Dev

How to extract the year from date (mm/dd/yyyy)

From Dev

How to extract tuples from a pandas symmetric dataframe

From Dev

How to extract multiple numbers from Pandas Dataframe

From Dev

How can I efficiently move from a Pandas dataframe to JSON

From Dev

How to efficiently subtract each row from pandas dataframe?

From Dev

How to extract only year from date in vue js html and print only that year?

From Dev

Extract precise year from date object in R

From Dev

Extract month, day and year from date regex

From Dev

Extract Year from Date Using VBA in Excel

From Dev

Extract Month and Year From Date in R

From Dev

Extract precise year from date object in R

From Dev

Extract month, day and year from date regex

From Dev

extract year from date and calculate datedifference

From Dev

How to extract the year of a DATE and insert it in a new column?

From Dev

How to extract year and date out of a string in R

From Dev

How to construct the date from year and day of the year

From Dev

pandas extract list from dataframe

From Dev

extract values from pandas dataframe

From Dev

How to efficiently unfold a date range span in Pandas?

From Dev

extract date-month-year from retrieved date

From Dev

How to replace efficiently values on a pandas DataFrame?

From Dev

How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

Related Related

  1. 1

    How to extract only year from the date in dataframes?

  2. 2

    Extract year from date

  3. 3

    How to extract date , month and year from the date we pass to javascript?

  4. 4

    python pandas extract year from datetime --- df['year'] = df['date'].year is not working

  5. 5

    python pandas extract year from datetime: df['year'] = df['date'].year is not working

  6. 6

    python pandas extract year from datetime: df['year'] = df['date'].year is not working

  7. 7

    How to extract day, month, and year from date returned by QML Calender?

  8. 8

    How to extract the year from date (mm/dd/yyyy)

  9. 9

    How to extract tuples from a pandas symmetric dataframe

  10. 10

    How to extract multiple numbers from Pandas Dataframe

  11. 11

    How can I efficiently move from a Pandas dataframe to JSON

  12. 12

    How to efficiently subtract each row from pandas dataframe?

  13. 13

    How to extract only year from date in vue js html and print only that year?

  14. 14

    Extract precise year from date object in R

  15. 15

    Extract month, day and year from date regex

  16. 16

    Extract Year from Date Using VBA in Excel

  17. 17

    Extract Month and Year From Date in R

  18. 18

    Extract precise year from date object in R

  19. 19

    Extract month, day and year from date regex

  20. 20

    extract year from date and calculate datedifference

  21. 21

    How to extract the year of a DATE and insert it in a new column?

  22. 22

    How to extract year and date out of a string in R

  23. 23

    How to construct the date from year and day of the year

  24. 24

    pandas extract list from dataframe

  25. 25

    extract values from pandas dataframe

  26. 26

    How to efficiently unfold a date range span in Pandas?

  27. 27

    extract date-month-year from retrieved date

  28. 28

    How to replace efficiently values on a pandas DataFrame?

  29. 29

    How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

HotTag

Archive