如何过滤每列具有不同条件的DataFrame

问题描述 投票:0回答:1

我有一个 DataFrame,其中每列我只想显示基于索引的特定值,但每列的这些条件都不同。这就是它的样子:

data = {'a': [1,2,3,4,5],
        'b': [10,20,30,40,50],
        'c': [1,1,1,1,1]}
df = pd.DataFrame(data)

df:
   a   b  c
0  1  10  1
1  2  20  1
2  3  30  1
3  4  40  1
4  5  50  1

我现在想在索引<3 for 'a', index <2 for 'b' and index = 4 for 'c'. Resulting in:

处取值
     a     b    c
0  1.0  10.0  NaN
1  2.0  20.0  NaN
2  3.0   NaN  NaN
4  NaN   NaN  1.0

我尝试了以下方法:

import pandas as pd

df_a = df.loc[df.index<3, 'a']
df_b = df.loc[df.index<2, 'b']
df_c = df.loc[df.index==4, 'c']

df_result = pd.concat([df_a, df_b, df_c], axis=1)```

这给出了期望的结果,但是有没有更有效的方法来做到这一点?所以如果我有一个“<" condition and a list for the "=" condition, then could I create the resulting filter in one go? It is fine if the NaNs become zeros, because that is what I want in the end anyway.

python pandas
1个回答
0
投票

你的方法很好,你可以通过字典理解来自动化它,并且

operators

from operator import lt, eq

conditions = {'a': (3, lt),
              'b': (2, lt),
              'c': (4, eq),
             }

out = pd.concat(
    {c: df.loc[op(df.index, val), c] for c, (val, op) in conditions.items()},
    axis=1,
)

输出:

     a     b    c
0  1.0  10.0  NaN
1  2.0  20.0  NaN
2  3.0   NaN  NaN
4  NaN   NaN  1.0
© www.soinside.com 2019 - 2024. All rights reserved.