mercredi 17 novembre 2021

populate and filter values in a dataframe based on values in another column in the other dataframe

I have climate (Temperature and Precipitation) data for multiple cities. I wish to seperate values of snow and rain for precipitation based on the temperature values and compile them in another dataframe. I could compile the dataframe for all values of precipitation using this code:

i=0
os.chdir('...\\downloaded\\')
while i< len(x):
    smhi = pd.read_csv(x[i],sep='\t', lineterminator='\n',skiprows=11, delimiter=',', encoding='utf-8')
    filename = x[i].split('.')[0]
    file=int(filename)
    a = df.loc[df['Id'] == file, 'Name'].tolist()
    city = a[0]
    precipitation[city]=smhi['Precipitation']
    i+=1
precipitation.head()

I got this output:enter image description here

But, when I try to slice the dataframe, using only the temperature constrain, I get many nan values. Some thing goes wrong with this code:

#Iterate for Snow
i=0
os.chdir('...downloaded\\')
while i< len(x):
    smhi = pd.read_csv(x[i],sep='\t', lineterminator='\n',skiprows=11, delimiter=',', encoding='utf-8')
    filename = x[i].split('.')[0]
    file=int(filename)
    a = df.loc[df['Id'] == file, 'Name'].tolist()
    city = a[0]
    p=0
    while p<=263:
        if smhi['Temperature'][p] < -2: 
            precipitation1[city][p]=smhi['Precipitation'][p]
        else:
            precipitation1[city][p]==0
        p+=1
    i+=1
precipitation1.head()

I get this wierd output: enter image description here

Can anybody help me sort out the issue? The code should've worked for the while loop to populate my empty dataframe.

Aucun commentaire:

Enregistrer un commentaire