I am quite new to programming and I wanted to know if you could help me. I have a file that contains daily precipitation data from 2000 to 2020. I think with pandas
I could create a dataframe that contains only the values of the month of January but I can't understand how. Or maybe with CDO
create a new file that only contains those values. Could someone help me? I would appreciate it
Asked
Active
Viewed 739 times
1

ClimateUnboxed
- 7,106
- 3
- 41
- 86
2 Answers
5
The cdo
solution would be
cdo selmon,n in.nc out.nc
where n is the number of the month, in your case n=1, so
cdo selmon,1 in.nc out.nc
You can also specify a list of months or a range using the forward slash, e.g.
cdo selmon,3/5 in.nc out.nc

ClimateUnboxed
- 7,106
- 3
- 41
- 86
2
Steps -
- Add a month column to your dataframe
df['month']= pd.to_datetime(df['date']).dt.month
- Select the rows you want using the condition df.month == 1
df[(df['month'] == 1)]

Rajarshi Ghosh
- 452
- 1
- 9
-
Whe i used that they say me "key error: Date" – Fernando Primo Forgioni Feb 13 '22 at 18:26
-
import xarray as xr import pandas as pd ds = xr.open_dataset('C:/Users/ferfo/Desktop/2020.nc') df = ds.to_dataframe() df= pd.to_datetime(df['date']).dt.month This is my code – Fernando Primo Forgioni Feb 13 '22 at 18:27
-
and this is the dataframe: datetime lon lat crs precip – Fernando Primo Forgioni Feb 13 '22 at 18:28
-
what is the name of the date column in your dataset - if header are not included , then you have to say ... df.columns = [list of columns] ... the column names should be in correct order conforming to the dataset, after that you should be able to use the date column as 'date' and use the code – Rajarshi Ghosh Feb 14 '22 at 06:56