利用pands提取csv中的数据
前言
数据分析时候,需要将数据进行加载和存储,本文主要介绍和excel的交互。
从csv中提取n列数据写入到excl中
read_excel()
加载函数为read_excel(),其具体参数如下。
read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None,names=None, parse_cols=None, parse_dates=False,date_parser=None,na_values=None,thousands=None, convert_float=True, has_index_names=None, converters=None,dtype=None, true_values=None, false_values=None, engine=None, squeeze=False, **kwds)
常用参数解析:
- io : string, path object ; excel 路径。
- sheetname : string, int, mixed list of strings/ints, or None, default 0 返回多表使用sheetname=[0,1],若sheetname=None是返回全表 注意:int/string 返回的是dataframe,而none和list返回的是dict of dataframe
- header : int, list of ints, default 0 指定列名行,默认0,即取第一行,数据为列名行以下的数据 若数据不含列名,则设定 header = None
- skiprows : list-like,Rows to skip at the beginning,省略指定行数的数据
- skip_footer : int,default 0, 省略从尾部数的int行数据
- index_col : int, list of ints, default None指定列为索引列,也可以使用u”strings”
- names : array-like, default None, 指定列的名字。
to_excel()
存储函数为pd.DataFrame.to_excel(),注意,必须是DataFrame写入excel, 即Write DataFrame to an excel sheet。其具体参数如下:
to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='', float_format=None,columns=None, header=True, index=True, index_label=None,startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None)
常用参数解析(这部分有部分英文解释,英语渣渣,先放着)
- excel_writer : string or ExcelWriter object File path or existing ExcelWriter目标路径
- sheet_name : string, default ‘Sheet1’ Name of sheet which will contain DataFrame,填充excel的第几页
- na_rep : string, default ”,Missing data representation 缺失值填充
- float_format : string, default None Format string for floating point numbers
- columns : sequence, optional,Columns to write 选择输出的的列。
- header : boolean or list of string, default True Write out column names. If a list of string is given it is assumed to be aliases for the column names
- index : boolean, default True,Write row names (index)
- index_label : string or sequence, default None, Column label for index column(s) if desired. If None is given, andheader and index are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.
- startrow :upper left cell row to dump data frame
- startcol :upper left cell column to dump data frame
- engine : string, default None ,write engine to use - you can also set this via the options,io.excel.xlsx.writer, io.excel.xls.writer, andio.excel.xlsm.writer.
- merge_cells : boolean, default True Write MultiIndex and Hierarchical Rows as merged cells.
- encoding: string, default None encoding of the resulting excel file. Only necessary for xlwt,other writers support unicode natively.
- inf_rep : string, default ‘inf’ Representation for infinity (there is no native representation for infinity in Excel)
- freeze_panes : tuple of integer (length 2), default None Specifies the one-based bottommost row and rightmost column that is to be frozen
实例
准备
1.vscode
2.csv文件内容

2代码
import pandas as pd csv_name=r"D:\测试文件夹\test文件\test\csv数据提取\2020-4-22.csv" #file=pd.read_csv(csv_name,encoding="ISO-8859-1") #读取csv文件 df1=pd.read_csv(csv_name,usecols=[1]) df2=pd.read_csv(csv_name,usecols=[5]) df3=pd.read_csv(csv_name,usecols=[6]) df4=pd.read_csv(csv_name,usecols=[7]) writer = pd.ExcelWriter(r"D:\测试文件夹\test文件\test\csv数据提取\提取结果\2020-4-22.xls") # 定义一个向Excel写入数据的对象 df1.to_excel(writer,index=False) # 向该Excel中写入df1到Data1这个sheet df2.to_excel(writer,startcol=1,index=False) # 向该Excel中写入df2到Data2这个sheet df3.to_excel(writer,startcol=2,index=False) df4.to_excel(writer,startcol=3,index=False) writer.save() # 保存Excel表格 writer.close() # 关闭Excel表格
3结果

(和想要的还是有差距,日期提取过来没有格式了,第一行多了框线和粗体,待解决)
函数解析
参考
1.函数解析(https://blog.csdn.net/tongxinzhazha/article/details/78796952)
2.pandas如何将多个DataFrame写入同一个excl工作簿中(https://blog.csdn.net/midion9/article/details/89000131?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase)
3pandas如何将多个DataFrame写入一个excl例子(从中解决的 sheet重复用问题价格index=false参数)
(https://blog.csdn.net/qq_21578125/article/details/81111651)
里面有很详细的解释可以参考 我只记录了函数结构和参数解析
浙公网安备 33010602011771号