gpt4 book ai didi

python - 如何使用标签中包含 '-' 连字符的列的 pd.DataFrame 对象的查询方法?

转载 作者:行者123 更新时间:2023-12-01 09:21:54 26 4
gpt4 key购买 nike

我想获取 SWISS-PROT-ID 字段的所有非 nan ID,以及 idx_filterTrue 值> 领域。我可以考虑一百万种其他方法来做到这一点,但在处理数据帧中更复杂的查询时,我遇到了这种类型的反射。

有没有办法查询这些类型名称作为列的数据框?

data = {'BLATTNER-ID': {'G0-16600': 'b4714', 'G6866': 'b1615', 'G0-10751': 'b4712', 'G0-10752': 'b4713', 'G6335': 'b0608', 'G6177': 'b0307', 'G0-8892': 'b4599', 'G0-10596': 'b4605', 'EG12861': 'b1915', 'EG12303': 'b1100'}, 'NAME': {'G0-16600': 'ralA', 'G6866': 'uidC', 'G0-10751': 'agrA', 'G0-10752': 'agrB', 'G6335': 'ybdR', 'G6177': 'ykgF', 'G0-8892': 'yneM', 'G0-10596': 'ypaB', 'EG12861': 'yecF', 'EG12303': 'ycfH'}, 'SWISS-PROT-ID': {'G0-16600': np.nan, 'G6866': 'Q47706', 'G0-10751': np.nan, 'G0-10752': np.nan, 'G6335': 'P77316', 'G6177': 'P77536', 'G0-8892': 'A5A616', 'G0-10596': np.nan, 'EG12861': 'P0AD07', 'EG12303': 'P0AFQ7'}, 'idx_filter': {'G0-16600': False, 'G6866': True, 'G0-10751': False, 'G0-10752': False, 'G6335': True, 'G6177': False, 'G0-8892': False, 'G0-10596': True, 'EG12861': False, 'EG12303': False}}
df = pd.DataFrame(data)

# 1st tried this
df.query("SWISS-PROT-ID and idx_filter").index
# UndefinedVariableError: name 'SWISS' is not defined
# 2nd tried escape characters
df.query("SWISS\-PROT\-ID and idx_filter").index
# UndefinedVariableError: name 'SWISS' is not defined

# Expecting
# ["G6335","G6866"]

版本:

pandas: 0.22.0 # I don't want to upgrade b/c there is a serious bug in 0.23.0 that breaks one of my programs 
python: 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

最佳答案

从 pandas 0.25 开始,您将能够使用反引号转义列名称,这样您就可以这样做

df.query("`SWISS-PROT-ID` and idx_filter").index

公然窃取:Pandas query function not working with spaces in column names

关于python - 如何使用标签中包含 '-' 连字符的列的 pd.DataFrame 对象的查询方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50746726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com