Is there a query method or similar for pandas Series (pandas.Series.query())?

后端 未结 3 762
终归单人心
终归单人心 2021-02-18 20:02

The pandas.DataFrame.query() method is of great usage for (pre/post)-filtering data when loading or plotting. It comes particularly handy for method chaining.

<
3条回答
  •  青春惊慌失措
    2021-02-18 20:39

    Why not convert from Series to DataFrame, do the querying, and then convert back.

    df["Points"] = df["Points"].to_frame().query('Points > 100')["Points"]
    

    Here, .to_frame() converts to DataFrame, while the trailing ["Points"] converts to Series.

    The method .query() can then be used consistently whether or not the Pandas object has 1 or more columns.

提交回复
热议问题