• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

jwang106


脚踏实地,日拱一卒。 建立新的神经链接,可不像公园散步那样简单。
  • 博客园
  • 联系
  • 管理

View Post

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') # 透视表

posted on 2019-08-05 10:39  jwang106  阅读(112)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3