pandas

Pandas提供了 DataFrame.describe 方法查看数据摘要

res.describe()
[10 rows x 24 columns]
       askPrice1  askPrice2  ...    bidVolume4     bidVolume5
count       10.0       10.0  ...     10.000000      10.000000
mean     10425.0    10425.5  ...  51242.200000  268655.800000
std          0.0        0.0  ...   2552.554224    2552.554224
min      10425.0    10425.5  ...  49265.000000  265690.000000
25%      10425.0    10425.5  ...  49265.000000  265690.000000
50%      10425.0    10425.5  ...  49265.000000  270633.000000
75%      10425.0    10425.5  ...  54208.000000  270633.000000
max      10425.0    10425.5  ...  54208.000000  270633.000000

DataFrame.isnull() 方法查看数据表中哪些为空值,与它相反的方法是 DataFrame.notnull()

df.dropna(axis=1, how='all')

经过测试,在 DataFrame.replace() 中使用空字符串,要比默认的空值NaN节省一些空间;但对整个CSV文件来说,空列只是多存了一个“,”,所以移除的9800万 x 6列也只省下了200M的空间。进一步的数据清洗还是在移除无用数据和合并上。

使用 DataFrame.dtypes 可以查看每列的数据类型,Pandas默认可以读出int和float64,其它的都处理为object,需要转换格式的一般为日期时间。DataFrame.astype() 方法可对整个DataFrame或某一列进行数据格式转换,支持Python和NumPy的数据类型。

df['Name'] = df['Name'].astype(np.datetime64)

对数据聚合,我测试了 DataFrame.groupby 和 DataFrame.pivot_table 以及 pandas.merge ,groupby 9800万行 x 3列的时间为99秒,连接表为26秒,生成透视表的速度更快,仅需5秒。

df.groupby(['NO','TIME','SVID']).count() # 分组
fullData = pd.merge(df, trancodeData)[['NO','SVID','TIME','CLASS','TYPE']] # 连接
actions = fullData.pivot_table('SVID', columns='TYPE', aggfunc='count') # 透视表

原文地址:https://www.cnblogs.com/wangjiale1024/p/11301565.html