-1

This function is not working df.sort_index(ascending=False) in my code.

All my codes:

df = pd.read_csv('qwe2.csv', parse_dates=['date'], dayfirst=True)

# index sıralaması yap
df.sort_index(ascending=False)

# Seperatörleri değiştir
df.now=df.now.str.replace(",",".")
df.open=df.open.str.replace(",",".")
df.high=df.high.str.replace(",",".")
df.low=df.low.str.replace(",",".")
df.volume=df.volume.str.replace(",",".")
df.fark=df.fark.str.replace(",",".")

# Tarih formatını değiştir
df['date'] = df['date'].dt.strftime('%m/%d/%Y')

# M simgesini kaldır ve 1.000.000 ile çarp
for i in df['volume']:
    i_temp = i
    if str(i).endswith('K'):
        i = float(str(i)[:-1]) * 1000
        df['volume'].loc[df['volume'].values == i_temp] = i
    elif str(i).endswith('M'):
        i= float(str(i)[:-1]) * 1000000
        df['volume'].loc[df['volume'].values == i_temp] = i

df.to_csv('qwe2.csv', index=False)
print(df)

All codes work without problems. But the index is not sorted from largest to smallest. Where could the error be?

Thanks

Oguz Han
  • 1
  • 3
  • 2
    can you show us what the data looks like before and after ? – mrCopiCat Jun 01 '22 at 22:20
  • 3
    I think you just need : ```df = df.sort_index(ascending=False)``` – mahdilamb Jun 01 '22 at 22:22
  • 1
    Most pandas methods return a new dataframe or series, they don't modify in place. If you're not sure, read the documentation. – Barmar Jun 01 '22 at 22:24
  • 1
    @mahdilamb or `df.sort_index(ascending=False, inplace=True)` – wjandrea Jun 01 '22 at 22:27
  • 2
    For debugging help in the future, you need to make a [mre], which should help catch simple mistakes like this. That means including minimal code (since almost all the code here is irrelevant to the problem), example input, expected output, and actual output. For specifics, see [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Jun 01 '22 at 22:30

0 Answers0