按列表对csv文件进行分组

时间:2020-06-02 09:31:46

标签: python list csv

嗨,我在处理csv文件时想按模式进行分组。 这是我的csv文件:

[温度],[风扇],[模式],[百叶窗],[摆动]

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,   

我想要这样的输出并使用python进行编程

[模式],[温度],[风扇],[百叶窗],[摆动]

['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 个答案:

答案 0 :(得分:0)

python的pandas库就是答案。

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

您可以通过grouped.get_group(['dry'])或任何您希望的方式访问此groupby对象的元素