Rain
Rain

Reputation: 45

Matching index with file name

I have a question about indexing and value selection. I have a main python script where I loop a large amount of files. Each file is then used as input in my code to do some operations. For these operations I also need the value of Area per input (x,y). I made a dataframe for this with the input names (gauge_id) as index and a column with values of the Area. I want to select the value of Area for their corresponding gauge_id. The gauge_id's have the same name as input x. How can I select the gauge_id of df2 for the corresponding name of input x?

Example:

input x= 1001 then I want the value of df2 matching with index gauge_id 1001.

def dataprocessing(x,y):
    
    A = pd.read_csv('camels_br_location.txt', delimiter=' ', header=None, skiprows=1, names=['gauge_id','gauge_name','gauge_region','gauge_lat','gauge_lon','area_ana','area_gsim','area_gsim_quality'])
    A.columns = A.columns.str.replace(' ', ',')
    A.index = A['gauge_id']
    df2 = pd.DataFrame()
    df2['Area'] = A['area_gsim']
    Area = *10**3 #Area catchment in m2
    print(Area)

Upvotes: 1

Views: 257

Answers (1)

zswqa
zswqa

Reputation: 914

def dataprocessing(x):
    A = pd.read_csv(
      'camels_br_location.txt', 
      delimiter=' ', 
      header=None, 
      skiprows=1, 
      names=['gauge_id','gauge_name','gauge_region','gauge_lat','gauge_lon','area_ana','area_gsim','area_gsim_quality'],
      index_col='gauge_id')
 
    return A.loc[x, :]['area_gsim']*10**3

And result is:

x = 10100000
print(dataprocessing(x)) # return 883138500.0

Upvotes: 1

Related Questions