简体   繁体   中英

Pandas Check two data frame columns and access third value

I am very beginner to pandas though I solved same case with comparison here is data frame I was working

s= [
     {
         'from':0,
         'to':400000000,
         'value':0.01
     },
     {
        'from':400000001,
        'to':500000000,
        'value':0.015
    },
    {
        'from':500000001,
        'to':1000000000,
        'value':0.03
    },
    {
        'from':1000000001,
        'to':10000000000,
        'value':0.04
    }
 ]

I want to compare two fields ie from and to and get third value that is value suppose the car price range is between 0 and 400000000 then I may get commission of 1% ie 0.01 bla bla Any way of doing this in pandas way. Here is little thing I worked on but I got empty data series

df = pd.DataFrame(s)

df = df[(df['from']<=0) &(400000000<df['to'])]['value']
print(df)

I think correct way is with DataFrame.loc for select column by mask, also is change <= from < for second condition:

s = df.loc[(df['from']<=0) & (df['to']<=400000000), 'value']
print(s)
0    0.01
Name: value, dtype: float64

You just need to apply those conditions correctly. Since there is no value that satisfies both conditions. You can do:

df[df['from'].le(0) & df['to'].le(400000000)]['value']

And it will result in:

0    0.01
Name: value, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM