Home > Blockchain >  Rename the row and columns of a dataframe
Rename the row and columns of a dataframe

Time:12-17

I have extracted a NC file using python and after processing the data the final output is an array with the of (199, 314). I convert the array to data frame, but the rows and columns names (index) start from zero to 199 and 314, respectively.

from netCDF4 import Dataset
import numpy as np
import pandas as pd
data = Dataset('GolestanM.nc', 'r')
dims = data.dimensions
ndims = len(dims)
vars = data.variables
nvars = len(vars)
attrs = data.ncattrs
lon = data.variables['lon'][:]
lat = data.variables['lat'][:]
t = data.variables['time'][496]
fire = data.variables['FireMask'][496,:,:]                           
dataset = pd.DataFrame(fire)

However, I want to rename these index using the following format: Columns: first name 53.7042 and then 0.0083 until the name reach 56.3208

[0-->53.7042, 1-->53.7.25, ... , 314-->53.3208]

Rows: first name 38.1125 and then -0.0083 until the name reach 36.4625

[0-->38.1125, 1-->38.1042, ... , 199-->36.4625]

to do this I have the code bellow:

dataset = dataset.rename(index={0: "38.1125"})
dataset = dataset.rename(columns={0: "53.7042"})
dataset = dataset.rename(index = lambda x: x   (0.0083),
                         columns = lambda x: x   (0.0083))

However doin this give me the following error:

TypeError: can only concatenate str (not "float") to str

CAn any one help me with the probem.

CodePudding user response:

Idea is multiple x (columns or index names) in lambda function:

#sample data
dataset = pd.DataFrame(0, index=range(10), columns=range(10))


dataset = dataset.rename(index = lambda x: 38.1125 - 0.0083 * x ,
                          columns = lambda x: 53.7042    0.0083* x)

print (dataset)
         53.7042  53.7125  53.7208  53.7291  53.7374  53.7457  53.7540  \
38.1125        0        0        0        0        0        0        0   
38.1042        0        0        0        0        0        0        0   
38.0959        0        0        0        0        0        0        0   
38.0876        0        0        0        0        0        0        0   
38.0793        0        0        0        0        0        0        0   
38.0710        0        0        0        0        0        0        0   
38.0627        0        0        0        0        0        0        0   
38.0544        0        0        0        0        0        0        0   
38.0461        0        0        0        0        0        0        0   
38.0378        0        0        0        0        0        0        0   

         53.7623  53.7706  53.7789  
38.1125        0        0        0  
38.1042        0        0        0  
38.0959        0        0        0  
38.0876        0        0        0  
38.0793        0        0        0  
38.0710        0        0        0  
38.0627        0        0        0  
38.0544        0        0        0  
38.0461        0        0        0  
38.0378        0        0        0  
  • Related