【问题标题】:Filter of one dataframe using multiple columns of other dataframe in python使用python中其他数据框的多列过滤一个数据框
【发布时间】:2023-01-26 17:19:19
【问题描述】:

我有一个数据框(df1),它是我想从中过滤或提取部分数据的原始数据。我有另一个具有过滤条件的数据框(df2)。这里的问题是我的过滤条件列,如果空白应该跳过列条件并移动到其他列条件

下面的例子:

DF1:

City District Town Country Continent
NY WASHIN DC US America
CZCH SEATLLE DC CZCH Europe
NY NJ DC US S America
BO WASHIN NY US America

DF2:(过滤条件表)

City District Town Country Continent
NY DC

请注意,我将地区、国家和大洲列留空。因为我以后可能会或可能不会使用它。我无法删除这些列。

输出方向:应该是这样的

City District Town Country Continent
NY WASHIN DC US America
NY NJ DC US S America

所以基本上我需要一个过滤条件表,它将从我在过滤表中输入的字段的原始数据中提取信息。我无法更改/删除 DF2 中的列。如果我不需要过滤条件,我只能将该列留空。

提前致谢, 尼茨

【问题讨论】:

  • DF2 是多行?

标签: python pandas dataframe filter conditional-statements


【解决方案1】:

如果 DF2 总是一行:

df = df1.merge(df2.dropna(axis=1), how='left')
print (df)
1  CZCH  SEATLLE   DC    CZCH     Europe
2    NY       NJ   DC      US  S America
3    BO   WASHIN   NY      US    America

【讨论】:

    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 2021-09-09
    • 2019-08-19
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多