【问题标题】:pandas - is there any difference between df.loc[df['column_label'] == filter_value] and df[df['column_label'] == filter_value] [duplicate]熊猫 - df.loc[df['column_label'] == filter_value] 和 df[df['column_label'] == filter_value] [重复]
【发布时间】:2020-06-20 20:39:16
【问题描述】:

我正在学习 pandas,想了解按列值过滤 DataFrame 行的最佳做法。

根据https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html,建议使用优化的pandas数据访问方式如.loc

来自https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html 的示例-

df.loc[df['shield'] > 6]

但是,根据https://pandas.pydata.org/docs/getting_started/comparison/comparison_with_sql.html#where,可以使用像tips[tips['time'] == 'Dinner'] 这样的结构。

为什么省略了推荐的.loc?有什么区别吗?

【问题讨论】:

    标签: python pandas dataframe indexing


    【解决方案1】:

    使用.loc,您还可以正确设置一个值,因为不使用它会引发you are trying to set a value on a copy of a DataFrame 错误。为了从您的 DataFrame 中获取某些内容,可能存在性能差异,但我不知道。

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 1970-01-01
      • 2021-08-18
      • 2018-12-01
      • 2020-11-08
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多