【问题标题】:Problems filtering xlsx and csv data with pandas使用 pandas 过滤 xlsx 和 csv 数据时出现问题
【发布时间】:2022-01-08 23:11:09
【问题描述】:

我有一个关于如何使用 pandas 过滤 xlsx 和 csv 数据点的问题。

import pandas as pd
df = pd.read_excel("Wind.xlsx")
df.head()
filter = df[["WindSpeed"] > 2]
ew=df[filter]
print(ew)

为什么它不会返回大于2的值?

结果是如下错误:

TypeError: '>' 在 'list' 和 'int' 的实例之间不支持

【问题讨论】:

    标签: python pandas csv filter xlsx


    【解决方案1】:

    使用

    filter = df["WindSpeed"] > 2
    

    【讨论】:

    • 我已经尝试过了。然后它会返回以下错误:TypeError: '>' not supported between 'str' and 'int'
    • @FrederikN.Pedersen 您的错误表明风速列中的值不是数字。 df.dtypes 为该列返回什么?
    • 文件的第一行是标题 - 这可能是问题吗?
    • 可能你必须强制转换为'int'
    • 这应该是因为Windspeed 是字符串类型。试试filter = df["WindSpeed"].astype('int') > 2
    【解决方案2】:

    试试这个

    filter = df[df['WindSpeed'] > 2]
    

    【讨论】:

    • 我收到以下错误:TypeError: '>' not supported between 'str' and 'int'
    • 问题是 Windspeed 是 string 类型,所以你需要将它转换为 int 类型。所以你应该使用 df[df['WindSpeed'].astype(int) > 2]
    • 我有我的答案。感谢您的贡献:)
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2023-04-05
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多