Project

General

Profile

How to make a csv file with separated columns for different values?

Added by Slavko Georgievski 11 months 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 Alex Holt 11 months 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 11 months ago

Alex Holt wrote in RE: How to make a csv file with separated columns for dif...:

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']

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.

    (1-3/3)