How to sort CSV rows by a single column using PANDAS python

谁说我不能喝 提交于 2021-01-29 14:39:05

问题


Currently on my project I am trying to sort the rows of a CVS sheet by a singular column, I am using PANDAS and I have seen several examples posted all around the internet, however when trying to implement this myself I have been unable to.

db = pd.read_csv(databasefile, skip_blank_lines=True, names=['ExampleOne','ExampleTwo','ExampleThree','ExampleFour'], header=1)
db.drop_duplicates(inplace=True)

db.sort_values(by=['ExampleOne'], ascending=[True])

db.to_csv(databasefile, index=False)

In the code above my thought would be that I am turning a CSV into a dataframe for PANDAS to use, in that dataframe I am dropping any duplicated rows and am sorting by the ExampleOne Column. In the end I am sending that information back to the CSV. However, when viewing the CSV after the code runs with no errors the data is not sorted in any order.

Database CSV Link

Here is the CSV in a txt format, the first 60 or so rows are sorted but that is becuase earlier in this process I am combining multiple CSV's together into one CSV.

Thankyou for reading! I would appreciate any help or suggestions anyone would have me try out as this problem has been frustrating for me. Thanks again for reading!


回答1:


databasefile = r"path"
databasefile2 = r"path"
db = pd.read_csv(databasefile, skip_blank_lines=True, names=['ExampleOne','ExampleTwo','ExampleThree','ExampleFour'], header=1)
print(db['ExampleOne'])
db.drop_duplicates(inplace=True)
db.sort_values(by=['ExampleOne'], ascending=True).to_csv(databasefile, index=False)

Here is a solution to your problem.



来源:https://stackoverflow.com/questions/58068265/how-to-sort-csv-rows-by-a-single-column-using-pandas-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!