pandas 加载 parquet 文件时过滤器中的布尔逻辑

biswetbf  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(135)

我想删除那些出生于1900年但尚未死亡的人。
下面的代码工作,但我需要两个过滤器来删除特定的行。有没有更简单的方法可以用一个过滤器删除行?
要复制的最少代码:

import pandas as pd

data = [
    (1900, None,),  # needs to be removed
    (1900, 2000,),
    (2000, None,),
    (2000, 2020,),
]
df = pd.DataFrame(data, columns=['birth', 'death'])
df.to_parquet('test.parquet')

# Rows which do not match the filter predicate will be removed
filters= [
    [
        ('birth', '!=', 1900),
    ],
    [
        ('birth', '=', 1900),
        ('death', 'not in', [None]),
    ]
]

df2 = pd.read_parquet('test.parquet', filters=filters)
df2.head()

文件:https://arrow.apache.org/docs/python/generated/pyarrow.parquet.read_table.html#pyarrow.parquet.read_table

y0u0uwnf

y0u0uwnf1#

实际上你不需要('birth', '=', 1900)条件,你可以保留(NOT BIRTH == 1900) OR (DEATH NOT IN NONE)的行,相当于NOT (BIRTH == 1900 AND DEATH IN NONE)

filters= filters= [[('birth', '!=', 1900)], [('death', 'not in', [None])]]

df2 = pd.read_parquet('test.parquet', filters=filters)

您还可以用途:

import pyarrow.compute as pc
filters = (pc.field('birth')!=1900) | ~pc.field('death').isin([None])

# or 
filters = ~( (pc.field('birth')==1900) & pc.field('death').isin([None]) )

输出:

birth   death
0   1900  2000.0
1   2000     NaN
2   2000  2020.0

相关问题