pandas - 'dataframe' object has no attribute 'str'

前端 未结 1 1221
南笙
南笙 2020-12-20 17:03

I am trying to filter out the dataframe that contains a list of product. However, I am getting the pandas - \'dataframe\' object has no attribute \'str\' error whenever I r

相关标签:
1条回答
  • 2020-12-20 17:48

    Short answer: change data.columns=[headerName] into data.columns=headerName

    Explanation: when you set data.columns=[headerName], the columns are MultiIndex object. Therefore, your log_df['Product'] is a DataFrame and for DataFrame, there is no str attribute.

    When you set data.columns=headerName, your log_df['Product'] is a single column and you can use str attribute.

    For any reason, if you need to keep your data as MultiIndex object, there is another solution: first convert your log_df['Product'] into Series. After that, str attribute is available.

    products = pd.Series(df.Product.values.flatten())
    include_clique = products[products.str.contains("Product A")]
    

    However, I guess the first solution is what you're looking for

    0 讨论(0)
提交回复
热议问题