Fede
Fede

Reputation: 1716

extract first day of month in dataframe

I want to extract the first day of month in a DF. But sometimes the first day is 2nd or 3th. For example

2006-01-01  2775.0  2825.0  2700.0  2725.0  10727600      
2006-01-02  2725.0  2750.0  2675.0  2675.0   2850000      
...
2006-03-03  2700.0  2825.0  2700.0  2825.0   4797600      
2006-03-04  2850.0  2900.0  2825.0  2900.0   5519200      
2006-03-05  2700.0  2825.0  2700.0  2825.0   4797600      
...
2006-04-02  2850.0  2900.0  2825.0  2900.0   5519200 
2006-04-03  1850.0  2900.0  2825.0  2900.0   5513100 

Upvotes: 0

Views: 582

Answers (2)

BENY
BENY

Reputation: 323226

Assumed you date columns name as date

#df.date=pd.to_datetime(df.date)
df=df[~df.date.dt.strftime('%Y-%m').duplicated()].copy()

Upvotes: 2

Umar.H
Umar.H

Reputation: 23099

One liner:

the idea is to group by each unique month and return the minimum datetime value.

#df[0] = pd.to_datetime(df[0]) #if necessary.
df.loc[df[0].isin(df.groupby(df[0].dt.month)[0].min())]

print(df)

           0       1       2       3       4         5
0 2006-01-01  2775.0  2825.0  2700.0  2725.0  10727600
2 2006-03-03  2700.0  2825.0  2700.0  2825.0   4797600
5 2006-04-02  2850.0  2900.0  2825.0  2900.0   5519200

Upvotes: 0

Related Questions