Having trouble resorting a CSV file according to a different columns contents

Viewed 28

Hey everyone I have been trying to sort a CSV file I requested from coin Market Caps API. After I got the data into a CSV file I then tried to make a new data frame called new_list that would sort the data from highest to lowest according to daily volume_24h.

Here is the first CSV file saved in the list variable.

list = pd.read_csv("crypto_latests.csv")

First what I tried was making this loop.

for item in list:   
    pd.concat(
    list.loc[:,'slug'],     
    list.loc[:,'volume_24h'], #['quote']['USD']['volume_24h'],    
    list.loc[:,'market_cap'], #['quote']['USD']['market_cap'],    
    list.loc[:,'last_updated']) #['quote']['USD']['last_updated']])
    new_list = list.sort_values(["volume_24h"], axis=0, ascending=[False], inplace=True)
    print(new_list)

When that did'nt work I tried another loop which just used the sort_value() function.

for item in list:
    new_list = list.sort_values(["volume_24h"], axis=0, ascending=[False], inplace=True)
    pd.concat(new_list)
    enter code here

When I run this code nothing is printed out and I get this error message.

c:\Users\rolle\OneDrive\Desktop\API\hightest_volume.py:8: FutureWarning: In a future version of pandas all arguments of concat except for the argument 'objs' will be keyword-only.
  pd.concat(
Traceback (most recent call last):
  File "c:\Users\rolle\OneDrive\Desktop\API\hightest_volume.py", line 8, in <module>
    pd.concat(
  File "C:\Users\rolle\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper     
    return func(*args, **kwargs)
  File "C:\Users\rolle\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\reshape\concat.py", line 347, in concat   
    op = _Concatenator(
  File "C:\Users\rolle\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\reshape\concat.py", line 382, in __init__ 
    raise TypeError(
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"
PS C:\Users\rolle\OneDrive\Desktop\API> 

Can anyone explain to me why my loops are not sorting my data from highest to lowest volume. Thank you.

Here is the Header from the first data frame

    > <bound method NDFrame.head of         
           id        slug    market_cap    volume_24h         last_updated
    0        0     bitcoin  3.796668e+11  3.019350e+10  2022-09-17 07:52:00
    1        1    ethereum  1.750225e+11  1.529217e+10  2022-09-17 07:52:00
    2        2      tether  6.792220e+10  4.401382e+10  2022-09-17 07:52:00
    3        3    usd-coin  5.023254e+10  5.144542e+09  2022-09-17 07:52:00
    4        4         bnb  4.457731e+10  7.364934e+08  2022-09-17 07:52:00
    ...    ...         ...           ...           ...                  ...
    4996  4996    minidoge  0.000000e+00  9.972073e+03  2022-09-17 07:52:00
    4997  4997      solarr  0.000000e+00  9.953524e+03  2022-09-17 07:52:00
    4998  4998  thoreum-v2  0.000000e+00  9.929755e+03  2022-09-17 07:52:00
    4999  4999  happy-fans  0.000000e+00  9.927134e+03  2022-09-17 07:52:00
1 Answers

Sorting in pandas is easy. You don't need a loop etc. Check if your column "volume_24h" is in correct datatype (should be float64)

Example:

import pandas as pd

df = pd.DataFrame(
                    {'volume_24h': [1.529217e+10, 7.364934e+08, 9.927134e+03, 3.019350e+10],
                    'col1':["a", "b", "c", "d"]
                    }
                  )
df.info()
print(df)
df.sort_values(by="volume_24h", inplace=True, ascending=False)
print("After sort:")
print(df)

Outputs:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   volume_24h  4 non-null      float64
 1   col1        4 non-null      object 
dtypes: float64(1), object(1)
memory usage: 192.0+ bytes
     volume_24h col1
0  1.529217e+10    a
1  7.364934e+08    b
2  9.927134e+03    c
3  3.019350e+10    d
After sort:
     volume_24h col1
3  3.019350e+10    d
0  1.529217e+10    a
1  7.364934e+08    b
2  9.927134e+03    c
Related