How to extract rows(from csv files) in a specific condition in python (csv files)

Viewed 125

Can you help me how I get the rows with each index value? Getting empty dataframe even though I set a condition.

I am trying to extract rows that have a certain index value. It is temperature data. I want to group the data based on temperature ranges. So, I did indexing for grouping the temperature data Then try to extract rows with a specific idx.

Here is entire code.

from pandas import read_csv
import pandas as pd
import numpy as np
from matplotlib import pyplot
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
bins = [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40] #21
path = '/Users/user/Desktop/OJERI/00_Research/00_Climate_Data/00_Temp/01_ASOS_daily_seperated/StationINFO_by_years/'
filename = f'STATIONS_LATLON_removedst_1981-2020_59.csv'    #59for1981-2020
df = pd.read_csv(path+filename, index_col=None, header=0)
station = df['Stations']
Temp = ['Tmean','Tmax','Tmin']`
for st in station[24:25]:
    for t in Temp:
        path1 = f'/Users/user/Desktop/OJERI/00_Research/00_Climate_Data/00_Temp/01_ASOS_daily_seperated/{t}_JJAS_NDJF/'
        filenames = f'combined_SURFACE_ASOS_{st}_1981-2020_JJAS_{t}.csv'
        df1 = read_csv(path1+filenames, index_col=None, header=0, parse_dates=True, squeeze=True)              
        def bincal(row):
            for idx, which_bin in enumerate(bins):
                if idx == 0:
                    pass
                if row <= which_bin:
                    return idx
        df1['data'] = df1[f'{t}'].map(bincal)
        print(df1)
        ok = df1[df1['data'] == 'idx']
        print(ok)

printing df1=read_csv looks like: print df1=read_csv

printing df1 after "df1['data'] = df1[f'{t}'].map(bincal)" looks like: printing df1 after "df1['data'] = df1[f'{t}'].map(bincal)"

I expected to get Date, Tmean(or Tmin, or Tmax), idx (which should same number) values when I print out "ok" however, it shows empty dataframe.

printing "ok"

Can you help me how I get the rows with each index value?

0 Answers
Related