涉县手机网站建设,深圳国内网站建设,网站定制微安电力案例,制作免费企业宣传视频文章目录 pandas介绍为什么使用pandasDataFrameDataFrame属性DataFrame的索引修改行列的索引值重设索引值以某列设置新索引 MultiIndexSerias索引操作直接索引按名字索引按数值索引 赋值操作排序对内容排序按索引排序 DataFrame的运算算术运算逻辑运算逻辑运算符号 |… 文章目录 pandas介绍为什么使用pandasDataFrameDataFrame属性DataFrame的索引修改行列的索引值重设索引值以某列设置新索引 MultiIndexSerias索引操作直接索引按名字索引按数值索引 赋值操作排序对内容排序按索引排序 DataFrame的运算算术运算逻辑运算逻辑运算符号 | 逻辑运算函数 query() isin() 统计运算自定义运算Pandas画图scv文件读取与存储hdf5文件读取与存储json文件读取与存储总结 pandas介绍
pandas panel data analysis 面板数据分析panel面板数据-计量经济学 三维数据以numpy为基础借力numpy模块在计算方面性能高的优势基于matplotlib能够简便的画图独特的数据结构
为什么使用pandas
便捷的数据处理能力读取文件方便封装了matplotlib、numpy的画图和计算能力
DataFrame
## 结构既有行索引又有列索引的二维数组
import pandas as pd
import numpy as np
stock_change np.random.normal(0, 1, (10, 5))
stock_changearray([[ 0.52652359, -0.42210135, 0.45506419, -0.1319933 , -0.85892243],[-2.80978824, 0.68502373, -0.72809275, -1.56716962, 0.24278934],[ 0.1423945 , -0.14913827, -0.30118759, 0.80841083, 0.56448585],[-1.11053808, -0.91833131, -0.82696531, 0.33592674, -1.81590623],[-0.7972349 , -0.38960542, -0.64822525, -1.67732846, -1.1320404 ],[-0.83075257, -0.96589613, 1.21458607, -0.54116531, 0.5416992 ],[ 0.2346827 , 0.38728822, 0.5534352 , 0.49615629, 0.03958449],[ 1.32743523, 0.8559906 , -0.35473279, -0.40734067, 0.23585156],[ 2.217162 , 0.43897264, 1.39278121, -0.17076621, 1.25111371],[-1.84123059, -1.00666366, 2.07583716, 1.03959872, 1.20092384]])stock_change1 pd.DataFrame(stock_change) ## 添加默认的索引
stock_change1012340-0.230423-0.1086772.116127-0.405135-0.60045711.422377-1.136674-0.4623350.795195-0.01326520.708261-0.197826-0.177992-1.0787430.3579873-0.3254320.2643370.856580-1.035939-0.22825240.0167341.0075540.4549110.252380-0.6919055-0.4717900.557541-0.7031710.344268-0.0832056-0.013339-0.3003711.4249160.0283381.10167070.061438-0.802730-0.746614-0.919655-1.33646480.3692740.5154270.661126-0.550260-1.5606339-1.087217-1.164305-0.4087481.198835-0.389584
# 添加行索引
stock_code [股票{}.format(i1) for i in range(10)]
stock_code
pd.DataFrame(stock_change, indexstock_code) ## 这里需要注意第一个参数是ndarray不是DataFrame结构否则数据会变为nan01234股票1-1.7961490.0634690.922334-0.3382072.157024股票2-0.0642180.9694530.223896-0.795105-2.020499股票3-0.0392860.046665-0.408812-0.2841451.852426股票4-1.8116170.588799-1.020581-0.421300-1.068160股票5-0.8671870.0702690.3624120.5958100.005319股票6-2.3842850.185213-0.0942010.5597061.156052股票71.2313960.226930-0.2845441.056286-0.765503股票81.451832-0.5184950.1155100.5782330.174324股票91.184461-0.327693-1.4054331.4804700.049133股票100.8913090.780864-0.858295-1.1544740.127319 pd.date_range(startNone,endNone,periodsNone,freq‘B’) start : 开始时间 end : 结束时间 periods : 时间天数 freq : 递进单位默认1天B’默认略过周末
date pd.date_range(start20231021, endNone, periods5, freqB)
dateDatetimeIndex([2023-10-23, 2023-10-24, 2023-10-25, 2023-10-26,2023-10-27],dtypedatetime64[ns], freqB)stock_c pd.DataFrame(stock_change, indexstock_code, columnsdate)
stock_c2023-10-232023-10-242023-10-252023-10-262023-10-27股票1-1.7961490.0634690.922334-0.3382072.157024股票2-0.0642180.9694530.223896-0.795105-2.020499股票3-0.0392860.046665-0.408812-0.2841451.852426股票4-1.8116170.588799-1.020581-0.421300-1.068160股票5-0.8671870.0702690.3624120.5958100.005319股票6-2.3842850.185213-0.0942010.5597061.156052股票71.2313960.226930-0.2845441.056286-0.765503股票81.451832-0.5184950.1155100.5782330.174324股票91.184461-0.327693-1.4054331.4804700.049133股票100.8913090.780864-0.858295-1.1544740.127319
DataFrame属性
对象.shape 获取形状对象.index 获取行索引对象.columns 获取列索引对象.values 获取值对象.T 获取行列转换对象.head() 查看前几行默认是5对象.tail() 查看最后几行 默认是5
stock_c.shape(10, 5)stock_c.indexIndex([股票1, 股票2, 股票3, 股票4, 股票5, 股票6, 股票7, 股票8, 股票9, 股票10], dtypeobject)stcok_c.columnsDatetimeIndex([2023-10-23, 2023-10-24, 2023-10-25, 2023-10-26,2023-10-27],dtypedatetime64[ns], freqB)stock_c.valuesarray([[-1.7961491 , 0.06346948, 0.92233413, -0.33820729, 2.15702396],[-0.06421753, 0.96945298, 0.22389647, -0.79510515, -2.02049945],[-0.03928641, 0.04666511, -0.40881248, -0.28414454, 1.85242648],[-1.81161734, 0.5887991 , -1.02058093, -0.42130023, -1.06816 ],[-0.86718681, 0.07026887, 0.36241195, 0.59581008, 0.00531913],[-2.38428482, 0.18521273, -0.09420118, 0.55970591, 1.15605167],[ 1.23139579, 0.22693018, -0.28454449, 1.05628637, -0.76550258],[ 1.45183169, -0.51849484, 0.11550995, 0.57823283, 0.17432416],[ 1.18446114, -0.3276933 , -1.40543347, 1.48046993, 0.04913251],[ 0.89130874, 0.78086438, -0.85829505, -1.15447368, 0.12731851]])stock_c.T股票1股票2股票3股票4股票5股票6股票7股票8股票9股票102023-10-23-1.796149-0.064218-0.039286-1.811617-0.867187-2.3842851.2313961.4518321.1844610.8913092023-10-240.0634690.9694530.0466650.5887990.0702690.1852130.226930-0.518495-0.3276930.7808642023-10-250.9223340.223896-0.408812-1.0205810.362412-0.094201-0.2845440.115510-1.405433-0.8582952023-10-26-0.338207-0.795105-0.284145-0.4213000.5958100.5597061.0562860.5782331.480470-1.1544742023-10-272.157024-2.0204991.852426-1.0681600.0053191.156052-0.7655030.1743240.0491330.127319
stock_c.head()2023-10-232023-10-242023-10-252023-10-262023-10-27股票1-1.7961490.0634690.922334-0.3382072.157024股票2-0.0642180.9694530.223896-0.795105-2.020499股票3-0.0392860.046665-0.408812-0.2841451.852426股票4-1.8116170.588799-1.020581-0.421300-1.068160股票5-0.8671870.0702690.3624120.5958100.005319
stock_c.tail()2023-10-232023-10-242023-10-252023-10-262023-10-27股票6-2.3842850.185213-0.0942010.5597061.156052股票71.2313960.226930-0.2845441.056286-0.765503股票81.451832-0.5184950.1155100.5782330.174324股票91.184461-0.327693-1.4054331.4804700.049133股票100.8913090.780864-0.858295-1.1544740.127319
DataFrame的索引
修改行列的索引值
stock_c.index [f股票_{i1} for i in range(10)]
## 不能直接索引改变
## stock_c.index[2] 123 ## pandas不支持这样的索引
stock_c2023-10-232023-10-242023-10-252023-10-262023-10-27股票_1-1.7961490.0634690.922334-0.3382072.157024股票_2-0.0642180.9694530.223896-0.795105-2.020499股票_3-0.0392860.046665-0.408812-0.2841451.852426股票_4-1.8116170.588799-1.020581-0.421300-1.068160股票_5-0.8671870.0702690.3624120.5958100.005319股票_6-2.3842850.185213-0.0942010.5597061.156052股票_71.2313960.226930-0.2845441.056286-0.765503股票_81.451832-0.5184950.1155100.5782330.174324股票_91.184461-0.327693-1.4054331.4804700.049133股票_100.8913090.780864-0.858295-1.1544740.127319
重设索引值
## stock_c.reset_index(dropTrue) 当dropTrue就会删除之前的索引为Fasle就不会删除之前的索引
stock_c.reset_index()index2023-10-23 00:00:002023-10-24 00:00:002023-10-25 00:00:002023-10-26 00:00:002023-10-27 00:00:000股票_1-1.7961490.0634690.922334-0.3382072.1570241股票_2-0.0642180.9694530.223896-0.795105-2.0204992股票_3-0.0392860.046665-0.408812-0.2841451.8524263股票_4-1.8116170.588799-1.020581-0.421300-1.0681604股票_5-0.8671870.0702690.3624120.5958100.0053195股票_6-2.3842850.185213-0.0942010.5597061.1560526股票_71.2313960.226930-0.2845441.056286-0.7655037股票_81.451832-0.5184950.1155100.5782330.1743248股票_91.184461-0.327693-1.4054331.4804700.0491339股票_100.8913090.780864-0.858295-1.1544740.127319
stock_c.reset_index(dropTrue)2023-10-232023-10-242023-10-252023-10-262023-10-270-1.7961490.0634690.922334-0.3382072.1570241-0.0642180.9694530.223896-0.795105-2.0204992-0.0392860.046665-0.408812-0.2841451.8524263-1.8116170.588799-1.020581-0.421300-1.0681604-0.8671870.0702690.3624120.5958100.0053195-2.3842850.185213-0.0942010.5597061.15605261.2313960.226930-0.2845441.056286-0.76550371.451832-0.5184950.1155100.5782330.17432481.184461-0.327693-1.4054331.4804700.04913390.8913090.780864-0.858295-1.1544740.127319
以某列设置新索引
df pd.DataFrame({year:[2021, 2021, 2023, 2024],month:[1, 2, 3, 4],sale:[22, 100, 222, 113]})
dfyearmonthsale02021122120212100220233222320244113
df.indexRangeIndex(start0, stop4, step1)## set_index(keys, dropTrue) keys列索引名称或者列索引名称列表 drop表示是否将列索引数据删除
df.set_index(keys[year])monthsaleyear2021122202121002023322220244113
new_df df.set_index(keys[year, month], dropFalse)
new_dfyearmonthsaleyearmonth20211202112222021210020233202332222024420244113
new_df.indexMultiIndex([(2021, 1),(2021, 2),(2023, 3),(2024, 4)],names[year, month])MultiIndex
new_df.index.namesFrozenList([year, month])tuples [(bar, one),(bar, two),(baz, one),(baz, two),(foo, one),(foo, two),(qux, one),(qux, two)]
index pd.MultiIndex.from_tuples(tuples, names[first, second])
indexMultiIndex([(bar, one),(bar, two),(baz, one),(baz, two),(foo, one),(foo, two),(qux, one),(qux, two)],names[first, second])pd.Series(np.random.randn(8), indexindex)first second
bar one -0.816907two 0.660782
baz one -1.032361two -0.595878
foo one -0.658145two -0.891936
qux one 0.385722two -0.192622
dtype: float64arrays [
np.array([bar, bar, baz, baz, foo, foo, qux, qux]),
np.array([one, two, one, two, one, two, one, two]),
]
df pd.DataFrame(np.random.randn(8, 4), indexarrays)
df0123barone-0.1627902.7991071.0706520.034360two-0.283814-0.551970-1.270871-0.813390bazone0.4221661.3801310.5938040.776062two1.888835-0.176970-0.568067-1.343601fooone-0.5329141.206831-0.3677050.912403two-1.576118-0.082882-0.1221761.521598quxone-0.074543-0.3592370.3097700.895598two0.9051860.670022-1.549954-0.539559
pd.DataFrame(np.random.randn(8, 4), indexindex)0123firstsecondbarone-1.208274-0.810972-1.820593-0.833156two-1.5016570.6838750.923321-0.710930bazone-0.008496-3.6450992.1257641.406796two-0.4406050.645926-1.6405361.002207fooone0.2647130.182264-1.4109300.837404two0.683733-0.3004261.2813740.440129quxone-0.179653-0.331090-0.8172770.583263two-0.305134-0.934428-0.479319-0.179533
MultiIndex.from_arrays()传入一个数组列表MultiIndex.from_tuples()传入一个元组数组、MultiIndex.from_product()传入一个交叉的迭代集合MultiIndex.from_frame()传入一个 DataFrame
Serias
对象[flag1][flag2][flag3] 先列后行对象.loc[] # 先行后列可以使用切片操作对象.iloc[] # 先行后列通过索引去进行索引
new_df[year][2021][1] ## 一定是先列后行2021df.loc[0:4, sale] ## 先行后列可以使用切片操作0 22
1 100
2 222
3 113
Name: sale, dtype: int64df.iloc[0:3, :5] ## 前3行前5列 先行后列通过索引去进行索引yearmonthsale02021122120212100220233222
new_df.iloc[0:3, :5]yearmonthsaleyearmonth2021120211222202121002023320233222
sr pd.Series(np.arange(2,10,2), index[数值{}.format(i1) for i in range(4)])
sr数值1 2
数值2 4
数值3 6
数值4 8
dtype: int32sr.valuesarray([2, 4, 6, 8])sr.indexIndex([数值1, 数值2, 数值3, 数值4], dtypeobject)索引操作
import numpy as np
import pandas as pd
mydata np.random.normal(0, 1, (5, 5))
mydata_index [index{}.format(i1) for i in range(5)]
mydata_col [col{}.format(i1) for i in range(5)]
data pd.DataFrame(mydata, indexmydata_index, columnsmydata_col)
datacol1col2col3col4col5index10.1789610.849560-0.077123-0.550173-0.821073index2-0.479774-0.986681-0.9347250.010318-0.736170index3-0.384807-0.6364850.056328-1.383175-0.451370index4-0.770427-1.009373-0.283575-0.923803-1.502639index50.068687-0.3612691.8277310.0348581.239907
直接索引
data[col1][index1] ## 先列后行-0.31201088599026405按名字索引
data.loc[index1][col1]-0.31201088599026405data.loc[index1, col1]-0.31201088599026405data.loc[[index1, index2], col1]index1 0.178961
index2 -0.479774
Name: col1, dtype: float64按数值索引
data.iloc[1, 0]-0.2269501796329433data.iloc[:4, :1]col1index10.178961index2-0.479774index3-0.384807index4-0.770427
赋值操作
data[col1] 0.01
datacol1col2col3col4col5index10.010.849560-0.077123-0.550173-0.821073index20.01-0.986681-0.9347250.010318-0.736170index30.01-0.6364850.056328-1.383175-0.451370index40.01-1.009373-0.283575-0.923803-1.502639index50.01-0.3612691.8277310.0348581.239907
data.col1 0.02
datacol1col2col3col4col5index10.020.849560-0.077123-0.550173-0.821073index20.02-0.986681-0.9347250.010318-0.736170index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
data.col1.index1 0.1
datacol1col2col3col4col5index10.100.849560-0.077123-0.550173-0.821073index20.02-0.986681-0.9347250.010318-0.736170index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
data[col1][index2] 0.3
datacol1col2col3col4col5index10.100.849560-0.077123-0.550173-0.821073index20.30-0.986681-0.9347250.010318-0.736170index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
排序
对内容排序
对象.sort_values(by, key, ascending) 单个键或者多个键进行排序默认升序 True升序 False降序
data.sort_values(by[col1], ascendingFalse)col1col2col3col4col5index20.30-0.986681-0.9347250.010318-0.736170index10.100.849560-0.077123-0.550173-0.821073index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
data.sort_values(by[col1, col2], ascendingFalse)col1col2col3col4col5index20.30-0.986681-0.9347250.010318-0.736170index10.100.849560-0.077123-0.550173-0.821073index50.02-0.3612691.8277310.0348581.239907index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639
sr data[col1] ## 对serias进行排序
srindex1 0.10
index2 0.30
index3 0.02
index4 0.02
index5 0.02
Name: col1, dtype: float64sr.sort_values()index3 0.02
index4 0.02
index5 0.02
index1 0.10
index2 0.30
Name: col1, dtype: float64按索引排序
对象.sort_index()
data.sort_index()col1col2col3col4col5index10.100.849560-0.077123-0.550173-0.821073index20.30-0.986681-0.9347250.010318-0.736170index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
sr.sort_index()index1 0.10
index2 0.30
index3 0.02
index4 0.02
index5 0.02
Name: col1, dtype: float64DataFrame的运算
算术运算
data.col1 2index1 2.10
index2 2.30
index3 2.02
index4 2.02
index5 2.02
Name: col1, dtype: float64data.col1.add(3)index1 3.10
index2 3.30
index3 3.02
index4 3.02
index5 3.02
Name: col1, dtype: float64data.sub(10).head(2) ## data - 10col1col2col3col4col5index1-9.9-9.150440-10.077123-10.550173-10.821073index2-9.7-10.986681-10.934725-9.989682-10.736170
data.col1.sub(data.col2).head(3)index1 -0.749560
index2 1.286681
index3 0.656485
dtype: float64逻辑运算
逻辑运算符号 |
## 筛选col1的数据大于0.1的
data.col1 0.1index1 False
index2 True
index3 False
index4 False
index5 False
Name: col1, dtype: booldata[data.col1 0.1]col1col2col3col4col5index20.3-0.986681-0.9347250.010318-0.73617
data[(data.col1 0.1) (data.col2 0.1)]col1col2col3col4col5index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
逻辑运算函数 query() isin()
## 对象.query(expr) expr 查询的字符串
data.query(col1 0.1 col2 0.1)col1col2col3col4col5index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
## 对象.isin(values) values数据列表 判断数据是否等于列表中的值
data[data.col1.isin([0.02, 0.01])]col1col2col3col4col5index30.02-0.6364850.056328-1.383175-0.451370index40.02-1.009373-0.283575-0.923803-1.502639index50.02-0.3612691.8277310.0348581.239907
统计运算
统计函数count、mean、std、min、max、var、prod、mode、abs、idmax、idmin上面的idmax、idmin、表示获取最小值最大值的位置 和numpy的argmax、argmin函数是类似的对象.describe() 一次性的获取平均值、标准差、最大值、最小值等值累计统计函数 cumsum、cummax、cummin、cumprod 分别是计算n个数的和、最大值、最小值、积
data.max()col1 0.300000
col2 0.849560
col3 1.827731
col4 0.034858
col5 1.239907
dtype: float64data.describe()col1col2col3col4col5count5.0000005.0000005.0000005.0000005.000000mean0.092000-0.4288500.117727-0.562395-0.454269std0.1213260.7632481.0289010.6101551.022660min0.020000-1.009373-0.934725-1.383175-1.50263925%0.020000-0.986681-0.283575-0.923803-0.82107350%0.020000-0.636485-0.077123-0.550173-0.73617075%0.100000-0.3612690.0563280.010318-0.451370max0.3000000.8495601.8277310.0348581.239907
data.col1.cumsum()index1 0.10
index2 0.40
index3 0.42
index4 0.44
index5 0.46
Name: col1, dtype: float64data.col1.cumsum().plot()
自定义运算
apply(func, axis0)
func:自定义函数 axis0:默认是列axis1表示进行行计算
# 计算col1和col2列最大值减去最小值的值
data[[col1, col2]].apply(lambda x: x.max() - x.min(), axis0)col1 0.280000
col2 1.858932
dtype: float64Pandas画图
Pandas.DataFrame.plot(xNone, yNone, kind‘line’) line折线图 bar柱状图 barh水平柱状图 hist直方图 pie饼图 scatter散点图 Pandas.Serias.plot
import pandas as pd
import numpy as np
mydata np.random.normal(0, 1, (5, 5))
mydata_index [index{}.format(i1) for i in range(5)]
mydata_col [col{}.format(i1) for i in range(5)]
data pd.DataFrame(mydata, indexmydata_index, columnsmydata_col)
datacol1col2col3col4col5index10.7067401.059931-0.2909750.4800270.869103index2-0.4610892.2782850.118369-0.141536-1.054914index30.871724-1.184708-0.729994-0.2911180.606099index4-0.300855-0.784571-1.8159730.7914390.861675index51.3804191.6757370.4000700.1302810.501257
data.plot()
data.plot(xcol1, ycol2, kindbarh)
scv文件读取与存储
pandas.read_csv(filepath_or_buffer, sep‘,’, header‘infer’, namesNone, usecols[])
DataFrame.to_csv(path_or_bufNone, sep‘,’, na_rep‘’, indexFalse, headerTrue, mode‘w’, encodingNone) path_or_buf写入CSV文件的路径或文件对象sep列分隔符默认为逗na_rep缺失值的表示默认为空字index是否写入行索引默认为 Falseheader是否写入列名默认为Truemode:写入模式 默认是w重写还有a追加模式
read_data pd.read_csv(E:/Project/PyCharm_Projects/pandas_test/read.csv, encodingGBK)
read_dataNameAge0李白211杜甫322孟浩然34
data {Name: [Alice, Bob, Carol],Age: [25, 30, 35]}
df pd.DataFrame(data)
df.to_csv(E:/Project/PyCharm_Projects/pandas_test/output.csv, indexFalse, modea, headerFalse)
df_read pd.read_csv(E:/Project/PyCharm_Projects/pandas_test/output.csv)
df_readNameAge0Alice251Bob302Carol353Alice254Bob305Carol35
hdf5文件读取与存储
pandas.read_hdf(path_or_buf, keyNone, **kwargs) path_or_buf 文件路径key:读取的键mode:打开模式return Theseselected objects DataFrame.to_hdf(path_or_buf, key, **kwargs)hdf5是使用键值对来存储数据的他也是可以存储三维数据的跨平台、支持压缩、节省空间
json文件读取与存储 pandas.read_json(path_or_bufNone, orientNone, type‘frame’, lines‘False’) 将json格式数据转换为默认的Pandas DataFrame格式的数据、orient:一般选择recordslines:是否把每行作为一个json DataFrame.to_json(path_or_bufNone, orientNone, lines‘False’)
总结
Pandas基础数据处理Pandas介绍 面板数据 数据处理工具 便捷的数据处理能力继承了Numpy和matplotlib读取文件方便Series一维数据 DataFrame多维数据Series属性index valuesDataFrame属性shape、index、columns、values、TDataFrame常用方法head() tail()Multiindex多维数据存储方式 Pandas基本操作 索引操作直接索引(先列后行)、按名字索引loc、按数字索引iloc赋值操作排序操作sort_values() sort_index() Pandas运算: 算术运算逻辑运算逻辑运算符 布尔索引 query() isin()统计运算describe()、min、max、std、idmax、idmin、cumsum、cummax自定义运算:apply() Pandas画图: df.plot()sr.plot() PandasIO操作: csv:pd.read_csv(path, names, usecols) pd.to_csv(path, header, mode, index)hdf5:pd.read_hdf5(path, key) pd.to_hdf5(path, key)json:pd.read_json(path, records, lines) pd.to_json(path, records, lines) ‘w’, encodingNone)path_or_buf写入CSV文件的路径或文件对象sep列分隔符默认为逗na_rep缺失值的表示默认为空字index是否写入行索引默认为 Falseheader是否写入列名默认为Truemode:写入模式 默认是w重写还有a追加模式