Python Forum
Save data frame to .csv df.to.csv()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save data frame to .csv df.to.csv()
#1
Hi everyone

I need to write a .csv file at each iteration of the following loop. I need to change the name of the .csv file at each iteration. The name of the file at each iteration is stored in the variable location. I´m getting this error: [Errno 2] No such file or directory: 'PONTE AGROAL (15G/02).csv'.
Can someone help me with this problem?
Thank you


f
from netCDF4 import Dataset
import numpy as np
import pandas as pd

# Reading the netcdf file
data = Dataset (r'G:\My Drive\Python_Projects\NC4 Files code\ERA5-Land hourly rad data from 1950 to present\ERA5_Surface solar radiation downwards_1980_2021.nc')
data_range = pd.date_range(start = '1980-01-01', end = '2022-01-01', freq='H')

location_lat_long = pd.read_csv('Stations_location.csv')

for index, row in location_lat_long.iterrows():
    location = row['Name']
    location_latitude = row['Latitude']
    location_longitude = row['Longitude']

    lat = data.variables['latitude'][:]
    lon = data.variables['longitude'][:]

    sq_diff_lat = (lat - location_latitude)**2
    sq_diff_lon = (lon - location_longitude)**2

    #Identifying the index of the minimum value for lat and lon
    min_index_lat = sq_diff_lat.argmin()
    min_index_lon = sq_diff_lon.argmin()


    df1 = pd.DataFrame(0, columns = ['ssrd'], index = data_range)

    df = df1.iloc[:-1 , :]

    dt = np.arange(0, data.variables['time'].size)

    temperature = data.variables['ssrd']

    for time_index in dt:
        df.iloc[time_index] = temperature[time_index, min_index_lat,min_index_lon]
        print (time_index)
    
    #Saving the time series to csv
    df.to_csv(location + '.csv')
Reply
#2
I have found this solution.

Replacing the last line with:

df.to_csv(f'Station_{index+1}.csv')
I will get:

Station_1.csv
Station_2.csv


It is not perfect because I still can´t use the name that is stored in

 location = row['Name']
, but it solves the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  how do you style data frame that has empty rows. gsaray101 0 498 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,162 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  Load multiple Jason data in one Data Frame vijays3 6 1,499 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 873 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Merging two Data Frame on a special case piku9290dgp 0 1,069 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Move a particular row in pandas data frame to last row klllmmm 0 3,642 Dec-27-2021, 09:11 AM
Last Post: klllmmm
  Iterating Through Data Frame Rows JoeDainton123 2 2,860 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,112 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Filtering a data frame according to number of occurences of an observation Menthix 1 1,851 May-31-2021, 10:50 PM
Last Post: supuflounder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020