Travis Cowart
Travis Cowart

Reputation: 121

Dataframe Sorting

I am working on Python pandas, beginning with sorting a dataframe I have created from a csv file. I am trying to create a for loop eventually, using values to compare. However, when I print the new values, they are using the original dataframe instead of the sorted version. How do I properly do the below?

Original CSV data:

date          fruit     quantity
4/5/2014 13:34  Apples      73
4/5/2014 3:41   Cherries    85
4/6/2014 12:46  Pears       14
4/8/2014 8:59   Oranges     52
4/10/2014 2:07  Apples      152
4/10/2014 18:10 Bananas      23
4/10/2014 2:40  Strawberries  98

Code:

import pandas as pd
import numpy

df = pd.read_csv('example2.csv', header=0, dtype='unicode')
df_count = df['fruit'].value_counts()
x = 0 #starting my counter values or position in the column
df.sort_values(['fruit'], ascending=True, inplace=True) #sorting the column 
fruit
print(df)
old_fruit = df.fruit[x]
new_fruit = df.fruit[x+1]
print(old_fruit) 
print(new_fruit)

Upvotes: 0

Views: 107

Answers (1)

A.Kot
A.Kot

Reputation: 7893

I believe you are still accessing the old index of x. After you sort, insert this to reindex:

df.reset_index(drop=True, inplace=True)

Upvotes: 1

Related Questions