zoukankan      html  css  js  c++  java
  • pandas 常用清洗数据(二)

    1、

    df.head()
    
    Here we import pandas using the alias 'pd', then we read in our data.
    
    df.head - shows us the first 5 rows and headers - it gives us an idea what to expect. df.tail - shows us the last 5 rows

    2、

    n [1]: df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
       ...:                     'B': ['B0', 'B1', 'B2', 'B3'],
       ...:                     'C': ['C0', 'C1', 'C2', 'C3'],
       ...:                     'D': ['D0', 'D1', 'D2', 'D3']},
       ...:                     index=[0, 1, 2, 3])
       ...: `
    
    In [2]: df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
       ...:                     'B': ['B4', 'B5', 'B6', 'B7'],
       ...:                     'C': ['C4', 'C5', 'C6', 'C7'],
       ...:                     'D': ['D4', 'D5', 'D6', 'D7']},
       ...:                      index=[4, 5, 6, 7])
       ...:
    
    In [3]: df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],
       ...:                     'B': ['B8', 'B9', 'B10', 'B11'],
       ...:                     'C': ['C8', 'C9', 'C10', 'C11'],
       ...:                     'D': ['D8', 'D9', 'D10', 'D11']},
       ...:                     index=[8, 9, 10, 11])

      
     
    
    In frames = [df1, df2, df3]
    In [5]: result = pd.concat(frames)
    result = df1.append([df2, df3])

    2、copy and value_counts

    df2 = df.copy()
    df2.DATE.value_counts().sort_index() //sort by index
    
    data_print = data['vote_count'].value_counts().sort_index()

    df['Amount'] = pd.to_numeric(df['Amount'])

    3, add  del

    del df2['column_name'] 
    del df2['column_name'] 
    del df2['column_name']
    
    df.insert(loc=0, column='Country', value='UK')
    data.insert(0, '性别', data.pop('gender'))#pop返回删除的列,插入到第0列,并取新名为'性别'

    4、筛选:

      

    bool = dt.str.contains   # df 是Series类型,不是DataFrame类型
                                       #返回的是True,False
    #获取筛选数据
    xuan_data =dt[bool]  #True的
  • 相关阅读:
    什么是代理
    简易的屏幕适配
    SourceTree 操作简介
    project/target/product的简易区别
    iOS申请真机调试证书-图文详解
    关于UINavigationController的backBarButtonItem定制问题
    pat1008
    pat1005
    pat1019-简单模拟题
    ie6-ie8中不支持opacity透明度的解决方法
  • 原文地址:https://www.cnblogs.com/cbugs/p/9888091.html
Copyright © 2011-2022 走看看