Pyhthon数据处理初步(一)

一、import packages
import pandas as pd #导入pandas
pd.set_option(‘precision’,5) #设置精度
pd.set_option(‘display_float.format’,lambda x:’%.5f’%x) #显示小数点后五位
pd.options.display.max_rows = 200 #最多显示200行
这里用到了pandas.set_option,具体查看官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.set_option.html

二、read data
data = pd.read_csv(‘filepath’,skiprows=0,header=1) #读取csv文件
data = pd.read_excel(‘filepath’,skiprows=0,header=1) #读取excel文件
这里用到了pandas.read_excel,具体请查看官方文档:
https://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.read_excel.html
注意:此时的data是一个DataFrame, DataFrame是什么?
DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表。
shiprows参数表示略去的行,header表示标题在第几行,从0开始

三、realize data
data.shape #查看数据行数和列数
data.head(3) #head()默认查看前五行数据,tail()默认查看后五行数据
set(data.dtypes) #利用dtypes方法查看DataFrame中各列的数据类型

#用select_dtypes方法将数据按数据类型进行分类,然后利用describe方法返回的统计值对数据有个初步的了解,另外添加了缺失值比重
data.select_dtypes(include=[‘0’]).describe().T
.assign(missing_pct=data.apply(lambda x:(len(x)-x.count())/len(x)))
注意:这里用到DataFrame.select_dtypes(),具体见官方文档:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.select_dtypes.html

assign 分配指派。 DataFrame.assign(), return:A new DataFrame with the new columns in addition to all the existing columns. 见文档:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html

data.select_dtypes(include=[‘float64’]).describe().T
.drop([‘25%’,‘50%’,‘75%’],axis=1)
.assign(missing_pct = data.apply(lambda x:(len(x)-x.count())/len(x)),
        nunique = data.apply(lambda x: x.nunique()),
        pct_10=data.select_dtypes(include=[‘float64’]).apply(lambda x:x.dropna().quantile(.1)),
        pct_25=data.select_dtypes(include=[‘float64’]).apply(lambda x:x.dropna().quantile(.25)),
        pct_50=data.select_dtypes(include=[‘float64’]).apply(lambda x:x.dropna().quantile(.5)),
        pct_75=data.select_dtypes(include=[‘float64’]).apply(lambda x:x.dropna().quantile(.75)),
        pct_90=data.select_dtypes(include=[‘float64’]).apply(lambda x:x.dropna().quantile(.9))
       )

你可能感兴趣的:(神经网络)