gpt4 book ai didi

python - 字符串问题/在 pandas 数据框中选择所有 > 8000 的值

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:59 25 4
gpt4 key购买 nike

我想在 pandas 数据框中选择所有大于 8000 的值。

new_df = df.loc[df['GM'] > 8000]

但是,它不起作用。我认为问题在于,该值来自 Excel 文件,并且该数字被解释为字符串,例如“1.111,52”。你知道我如何将这样的字符串转换为 float/int 以便正确比较它吗?

最佳答案

摘自 pd.read_excel 的文档:

Thousands separator for parsing string columns to numeric. Note that this parameter is only necessary for columns stored as TEXT in Excel, any numeric columns will automatically be parsed, regardless of display format.

这意味着 pandas 检查存储在 excel 中的格式的类型。如果这是 Excel 中的 numeric,则转换应该是正确的。如果您的列是 string,请尝试使用:

df = pd.read_excel('filename.xlsx', thousands='.')

如果你有一个 csv 文件,你可以通过指定 thousands + decimal 字符来解决这个问题:

df = pd.read_csv('filename.csv', thousands='.', decimal=',')

关于python - 字符串问题/在 pandas 数据框中选择所有 > 8000 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58179925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com