pandas 数据类型转换及描述统计

     处理数据的时候往往需要对原始数据进行类型转换和预览等操作,下面介绍常用的处理预览和数据转换方法

预览:例:

import pandas as pd
sec_weather = pd.read_table(r'D:weather.csv',sep=',')
sec_weather.head()

如果只需要预览数据的几行信息,可以使用head方法和tail方法。head方法返回数据集的开头5行,tail方法返回数据集的末尾5行。

还可以进一步查看数据集有多少观测和多少变量,以及每个月变量都是什么数据类型。例如:

print('数据集的行列数: ',sec_weather.shape)
print('各变量的数据类型: ',sec_weather.dtypes)

通过得出的结论可以看出各数据的类型,可对各数据类型进行进行转换

例如:

pd.to_datatime(sec_weather.xxx,format = '%Y年%m月')

sec_weather.xxx.str[:-1].astype('float')

sec_weather.dtypes

描述性统计  describe()

sec_weather.describe()

通过基本的统计量(如最小值,均值,中位数,最大值等)描述数据的特征。

原文地址:https://www.cnblogs.com/tinglele527/p/11904368.html