Home > Software engineering >  Pandas Check two data frame columns and access third value
Pandas Check two data frame columns and access third value

Time:05-20

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 i.e 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% i.e 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)

CodePudding user response:

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

CodePudding user response:

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
  • Related