【问题标题】:I need to get pandas-dataframe rows, that includes specific value on specific list's place in column [duplicate]我需要获取 pandas-dataframe 行,其中包括列中特定列表位置的特定值 [重复]
【发布时间】:2020-08-13 19:39:24
【问题描述】:

我有一个包含几列的数据框。此列的类型之一是列表:

import pandas as pd


df = pd.DataFrame({'col1': [[1, 2, 'x'], ['as', 3, 5, 7], [1, 2]],
                   'col2': ['barfoo', 'foo', 'bar']})
print(df)
            col1    col2
0      [1, 2, x]  barfoo
1  [as, 3, 5, 7]     foo
2         [1, 2]     bar 

所以我需要获取行,其中 'col1' 中列表的值等于 2。

类似这样的东西(它不起作用):

result = df.loc[df['col1'].list[1] == 2]

期望的输出:

            col1    col2
0      [1, 2, x]  barfoo
2         [1, 2]     bar 

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以这样做:

    df.loc[df.col1.str[1] == 2]
    

    【讨论】:

    • 大声笑。非常惊讶,它的工作原理。谢谢
    • 很高兴我能帮上忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2021-10-16
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多