Search code examples
mysqlsqlpandaspandas-groupbypandasql

SQL query execution using panda library


I have an SQL query like this "select (ShipMode),(count(OrderID)*100/8994) as Score from friends.sampledatapanda(I have a CSV file, so ignore this) group by 1". Which I want to execute the same using panda library on Jupyter. Please help.


Solution

  • You can use pandas' value_counts() method to count the number of values, and use the normalize=True parameter to get the frequencies. Assuming you have read your data into a DataFrame called df:

    df['Ship Mode'].value_counts(normalize=True)
    
    Out[3]:
    
    Standard Class    0.597158
    Second Class      0.194617
    First Class       0.153892
    Same Day          0.054333
    Name: Ship Mode, dtype: float64