Home > Software design >  Loop over multiple values stored in single column e.g [345 1234]
Loop over multiple values stored in single column e.g [345 1234]

Time:09-16

I have a CSV file in which one column has stored two values at one position such as [345 1234] but sometimes there is no value such as [] or only one [456]. I write a script that works well for empty or single values but it failed for double values. I tried to split, replace space with , but it gave an error because of an empty or single value. How can I include these two values through the loop which can work for both empty and single value?

df file format is as below:

20200423.GS022.mseed_597000,[ ],

20200423.GS022.mseed_600000,[ ],

20200423.GS022.mseed_603000,[424 1901],

20200423.GS022.mseed_606000,[2259],

#code for an empty or single value

with open('myfile.txt','w') as f:
for i in range(len(df)):
    itp = df.iloc[i, 1].replace('[', '').replace(']', '')
    if itp != '':
        P=int(float(itp))/100
    else:
        P=0
    

I got this error,

ValueError: could not convert string to float: '424 1901'

CodePudding user response:

The reason for the failure is you are converting itp to float however 424 1901 is a string with space in it and hence cannot be converted to float.
If 424 and 1901 are 2 different values you can use the below code:

with open('myfile.txt','w') as f:
    for i in range(len(df)):
        itp = df.iloc[i, 1].replace('[', '').replace(']', '')
        for val in itp.split():
            if val != '':
                P=int(float(val))/100
            else:
                P=0
            print (f'P: {P}')

However if both are a single value like 4241901 you can use the below code:

with open('myfile.txt','w') as f:
    for i in range(len(df)):
        itp = df.iloc[i, 1].replace('[', '').replace(']', '').replace(" ","")
        if itp != '':
            P=int(float(itp))/100
        else:
            P=0
  • Related