gpt4 book ai didi

python - Pandas 中的排序函数,返回困惑的数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:49 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在 Pandas 中使用排序功能对 CSV 文件中的数据进行排序。我在原始文件中有 229 行。但是排序后的输出是245行,因为一个字段中的一些数据被打印到下一行,而一些行没有任何值。

sample=pd.read_csv("sample.csv" , encoding='latin-1', skipinitialspace=True)
sample_sorted = sample.sort_values(by = ['rating'])
sample_sorted.to_csv("sample_sorted.csv")

我想,这个问题的发生是因为在某些单元格中,数据是通过生成新行输入的。例如,这是原始文件中单元格的内容。当我对原始文件进行排序时,第二行打印在一个新行中,第一行和第二行之间有 3 行留空。

"Side effects are way to extreme. 



E-mail me if you have experianced the same things."

有什么建议吗?谢谢!

最佳答案

您可以尝试删除问题列中的换行符。

sample=pd.read_csv("sample.csv" , encoding='latin-1', skipinitialspace=True)
sample["problem_column"] = (sample["problem_column"].
apply(lambda x: " ".join([word for word in x.split()])
)

看看是否有帮助。如果没有可重现的样本,很难理解为什么会发生这种情况。

关于python - Pandas 中的排序函数,返回困惑的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39337648/

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