Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按列表对csv文件进行分组_Python_List_Csv - Fatal编程技术网

Python 按列表对csv文件进行分组

Python 按列表对csv文件进行分组,python,list,csv,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,

嗨,我正在努力处理我的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']

python的熊猫库就是答案

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对象的元素,答案就是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对象的元素