How to make a csv file with separated columns for different values?
Added by Slavko Georgievski over 1 year ago
Hi all,
i need help i merged files with 4 parameters, tasmax,tasmin,pr,ETo and i have one file its only on grid and i need a function to make a csv file, but the values from this parameters should be in different columns
Replies (3)
RE: How to make a csv file with separated columns for different values? - Added by Karin Meier-Fleischer over 1 year ago
Please, don't do cross-postings. See https://code.mpimet.mpg.de/boards/1/topics/14606?r=14613
RE: How to make a csv file with separated columns for different values? - Added by Alex Holt over 1 year ago
import xarray as xr
import pandas as pd
data = xr.open_dataset('merged_file.nc')
tasmax = data['tasmax']
tasmin = data['tasmin']
pr = data['pr']
ETo = data['ETo']
RE: How to make a csv file with separated columns for different values? - Added by Alex Holt over 1 year ago
Alex Holt wrote in RE: How to make a csv file with separated columns for dif...:
import xarray as xr
import pandas as pddata = xr.open_dataset('merged_file.nc')
tasmax = data['tasmax']
tasmin = data['tasmin']
pr = data['pr']
ETo = data['ETo']
df = pd.DataFrame({'tasmax': tasmax.values.flatten(),
'tasmin': tasmin.values.flatten(),
'pr': pr.values.flatten(),
'ETo': ETo.values.flatten()})
df.to_csv('output.csv', index=False)
In the above code, the tasmax, tasmin, pr, and ETo variables represent the respective parameters from your merged NetCDF file. By combining them into a single DataFrame, you can save the DataFrame as a CSV file where each parameter will be in a separate column.