Timberghost_
Timberghost_

Reputation: 175

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

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!

Upvotes: 0

Views: 3232

Answers (1)

Jiraiya
Jiraiya

Reputation: 121

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.

Upvotes: 1

Related Questions