Home > other >  Python index value from list, return index value and list value
Python index value from list, return index value and list value

Time:11-05

I'm trying to use a list of specific codes to index any time one of those codes is used and then return the value of that code and the parameter name associated with it.

import numpy as np
import pandas as pd

param_list = pd.read_csv(r'C:/Users/Gordo/Documents/GraduateSchool/Research/GroundWaterML/parameter_cd_query.csv')

#def p_list():
#    return [param_list['p_cd'], param_list['param_nm']]

for item, value in param_list['p_cd'], param_list['parm_nm']:

    if item in ['p00010','p00020','p00025','p00058','p00059','p00090','p00095','p00191','p00300','p00301','p00400','p00405','p00410',
                   'p00450','p00452','p00453','p00602','p00607','p00608','p00613','p00618','p00631','p00660','p00666','p00671',
                   'p00681','p00900','p00904','p00905','p00915','p00925','p00930','p00931','p00932','p00935','p00940',
                   'p00945','p00950','p00955','p01000','p01005','p01010','p01020','p01025','p01030','p01035','p01040','p01046',
                   'p01049','p01060','p01065','p01080','p01085','p01090','p01106','p01130','p01145','p01155','p04035','p07000',
                   'p09511','p22703','p29801','p39086','p49933','p50624','p61028','p62636','p62639','p62642','p62645',
                   'p63041','p63162','p63790','p70300','p70301','p70303','p71846','p71851','p71856','p71865','p71870','p72015',
                   'p72016','p72019','p82081','p82082','p82085','p90095','p99832','p99833','p99834']:
       print (item, value)

CodePudding user response:

If I understand your question correctly, you have your own predefined codes and you're trying to see if items from a csv file matches any of your codes. If that's the case, you can get all the matches just by filtering the dataframe (since you're using pandas anyway).

import pandas as pd

param_df = pd.read_csv(r'C:/Users/Gordo/Documents/GraduateSchool/Research/GroundWaterML/parameter_cd_query.csv')
my_codes = ['p00010','p00020','p00025','p00058','p00059','p00090','p00095','p00191','p00300','p00301','p00400','p00405','p00410',
               'p00450','p00452','p00453','p00602','p00607','p00608','p00613','p00618','p00631','p00660','p00666','p00671',
               'p00681','p00900','p00904','p00905','p00915','p00925','p00930','p00931','p00932','p00935','p00940',
               'p00945','p00950','p00955','p01000','p01005','p01010','p01020','p01025','p01030','p01035','p01040','p01046',
               'p01049','p01060','p01065','p01080','p01085','p01090','p01106','p01130','p01145','p01155','p04035','p07000',
               'p09511','p22703','p29801','p39086','p49933','p50624','p61028','p62636','p62639','p62642','p62645',
               'p63041','p63162','p63790','p70300','p70301','p70303','p71846','p71851','p71856','p71865','p71870','p72015',
               'p72016','p72019','p82081','p82082','p82085','p90095','p99832','p99833','p99834']

result = param_df[param_df.p_cd.isin(my_codes)]

result gives you all matches. If you just want an array with the first match, you can do:

result.loc[0].values
  • Related