Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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:如何拆分按第一列分组的excel文件_Python_Excel_Pandas - Fatal编程技术网

python:如何拆分按第一列分组的excel文件

python:如何拆分按第一列分组的excel文件,python,excel,pandas,Python,Excel,Pandas,我有一个表,需要拆分为多个文件,按第1列-序列中的值分组 +--------+--------+-------+ | serial | name | price | +--------+--------+-------+ | 100-a | rdl | 123 | | 100-b | gm1 | -120 | | 100-b | gm1 | 123 | | 180r | xxom | 12 | | 182d | data

我有一个表,需要拆分为多个文件,按第1列-序列中的值分组

+--------+--------+-------+
| serial |  name  | price |  
+--------+--------+-------+
| 100-a  | rdl    |   123 |  
| 100-b  | gm1    |  -120 |  
| 100-b  | gm1    |   123 |  
| 180r   | xxom   |    12 |  
| 182d   | data11 | 11.50 |  
+--------+--------+-------+
输出如下所示:

100-a.xls
100-b.xls
180r.xls etc.etc.
打开100-b.xls包含以下内容:

+--------+------+-------+
| serial | name | price |  
+--------+------+-------+
| 100-b  | gm1  |  -120 |  
| 100-b  | gm1  |   123 |  
+--------+------+-------+
我尝试使用Pandas通过以下代码定义数据帧:

import pandas as pd
#from itertools import groupby

df = pd.read_excel('myExcelFile.xlsx')

我成功地获得了数据帧,但我不知道下一步该怎么做。我试着这样做,但情况有点不同。下一步是什么

这不是groupby,而是一个筛选器

您需要遵循两个步骤:

在excel文件中生成所需的数据 将dataframe另存为excel。 像这样的东西应该能奏效-

for x in list(df.serial.unique()) :
    df[df.serial == x].to_excel("{}.xlsx".format(x))