简体   繁体   English

Pandas 检查两个数据框列并访问第三个值

[英]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尽管我通过比较解决了相同的情况,但我对 pandas 非常陌生,这里是我正在工作的数据框

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.我想比较两个字段,即 from 和 to 并获得第三个值,即假设汽车价格范围在 0 到 400000000 之间,那么我可能会得到 1% 的佣金,即 0.01 bla bla 以熊猫方式执行此操作的任何方式。 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:我认为正确的方法是使用DataFrame.loc通过掩码选择列,对于第二个条件也可以更改<= from <

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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