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

标签 python sorting pandas

我正在尝试使用以下代码在 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/

相关文章:

python/pandas 从时间表到时间日期数据框

python - 使用 matplotlib 的 pcolormesh 时指定颜色

python - 如何在安装程序中安装 PyPi 的 Ubuntu 软件包?

java - 带有用于保持计数排序的索引的 PriorityQueue

swift - RxSwift 可观察数组排序

python - python pandas 中的双端队列

python - 如何使用 Pandas 将数据从一行移动到另一行

python - 将 .htm 或 .html 扩展名与 python RE 匹配

python - 在 Python 的构造函数中使用 self._variable 和仅使用 self.variable 有什么区别

c# - 对 List<T> 上的 Datagridview 数据源进行排序,其中 T 是匿名的