0

Below is the sample format of my data

[Daily Rainfall data][1]
Date              Rainfall
1/Jan/1981           0.832
2/Jan/1981           0.534
   -                   -
   -                   -
   -                   -
1/Mar/1981           2.364
   -                   -
1/June/1981          0.002
   -                   -
   -                   -
31/Dec/1981          5.324

From those data above I want to take only daily Rainfall data of March to May, I do not need other months Example I want to my data to be arranged as

Date              Rainfall
1/Mar/1981           8.832
2/Mar/1981           2.534
   -                   -
   -                   -
   -                   -
1/April/1981          4.36
   -                   -
1/May/1981            3.65
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Doris
  • 1

1 Answers1

0

First, you need to remove the punctuation "/" in the Date column with

df['Date'] = df['Date'].str.replace('/','') Then, change Date to dateime format for better editing:

df['Date'] = pd.to_datetime(df['Date'],format='%d%b%Y') Then, filter the months you need, for example here I just filter Jan & Feb :

df.loc[df['Date'].dt.month.between(1,2)]

Refer screenshot for complete code: Screenshot

  • To sort the date after the filtering process, refer this post: https://stackoverflow.com/questions/28161356/sort-pandas-dataframe-by-date – Iwanttobeatuna Jul 06 '21 at 09:59