Group the csv file by list

Hi I'm struggling with my csv file and want to group it by it's mode. Here's me csv file:

[Temperature],[Fan],[Mode],[Louver],[Swing]

16, auto,   cool,   auto,   auto,   
16, auto,   cool,   auto,   auto,   
16, auto,   cool,   auto,   auto,   
16, auto,   cool,   auto,   off,    
16, auto,   cool,   auto,   auto,   
16, auto,   cool,   off,    auto,   
16, auto,   cool,   auto,   auto,   
16, high,   cool,   auto,   auto,   
16, med,    cool,   auto,   auto,   
16, low,    cool,   auto,   auto,   
16, auto,   cool,   auto,   auto,   
17, auto,   cool,   auto,   auto,   
18, auto,   cool,   auto,   auto,   
19, auto,   cool,   auto,   auto,   
20, auto,   cool,   auto,   auto,   
21, auto,   cool,   auto,   auto,   
22, auto,   cool,   auto,   auto,   
23, auto,   cool,   auto,   auto,   
24, auto,   cool,   auto,   auto,   
25, auto,   cool,   auto,   auto,   
26, auto,   cool,   auto,   auto,   
27, auto,   cool,   auto,   auto,   
28, auto,   cool,   auto,   auto,   
29, auto,   cool,   auto,   auto,   
30, auto,   cool,   auto,   auto,   
29, auto,   cool,   auto,   auto,   
28, auto,   cool,   auto,   auto,   
27, auto,   cool,   auto,   auto,   
2,  auto,   dry,    auto,   auto,   
1,  auto,   dry,    auto,   auto,   
0,  auto,   dry,    auto,   auto,   
-1, auto,   dry,    auto,   auto,   
-2, auto,   dry,    auto,   auto,   
2,  auto,   auto,   auto,   auto,   
1,  auto,   auto,   auto,   auto,   
0,  auto,   auto,   auto,   auto,   
-1, auto,   auto,   auto,   auto,   
-2, auto,   auto,   auto,   auto,   

I want to have an output like this and program it using python

[Mode], [Temperature], [Fan], [Louver], [Swing]

['dry'], ['2', '1', '0', '-1', '-2'], ['auto'], ['auto'], ['auto']
['auto'], ['2', '1', '0', '-1', '-2'], ['auto'], ['auto'], ['auto']
['cool'], ['16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'], ['auto', 'high', 'med', 'low'], ['auto', 'off'], ['auto', 'off']

1 answer

  • answered 2020-06-02 09:47 Shahid Khan

    python's pandas library is the answer.

    import pandas as pd
    df = pd.read_csv(your_csv_path)
    grouped = df.groupby([Mode]) # you can group them in the sequence you like
    

    you can access elements of this groupby object, simply by grouped.get_group(['dry']) or whatever you may wish