0

I have a data set in csv that I read with pd.read_csv. I want to sort the esxisting data by descanding value.

my code is this:

dataset = pd.read_csv('Kripto.csv')
sorted = dataset.sort_values(by = "Freq1", ascending=False)

x = dataset.iloc[:, :].values

and my data set (print(dataset)) is this :

 Letter;Freq1

0      A;0.0817

1      B;0.0150

2      C;0.0278

3      D;0.0425

4      E;0.1270

when i want to use this code:

sorted = dataset.sort_values(by = "Freq1", ascending=False)

python gives me an error and says KeyError: 'Freq1'

I know that "Freq1" is not the name of the column but ı have no idea how to assing a name

2
  • 2
    dataset = pd.read_csv('Kripto.csv', sep=';') would fix your problem. Commented Mar 29, 2021 at 0:26
  • Note that sorting is almost only useful for printing (that is, for humans); especially when you are working with dataframes, since selections are normally done on other criteria than the order.
    – 9769953
    Commented Mar 29, 2021 at 0:31

2 Answers 2

1

Your csv file has " ; " as separator, you need to indicate on the read_csv method:

import pandas as pd
dataset = pd.read_csv('your.csv', sep=';') 

And that's all you need to do

1

Your CSV file uses semi-colons to separate values. Since Pandas by defaults expects comma's, use

dataset = pd.read_csv('Kripto.csv', sep=';') 

instead.

You should also use the sorted dataset to get your values in sorted order, instead of dataset, since the latter will remain unsorted:

x = sorted.iloc[:, :].values
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.