• 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的
  • 相关阅读:
    6-1面向对象
    5-1模块
    python随机数
    4-5目录
    4-4内置函数
    4-3迭代器和生成器
    4-1装饰器1
    4-2装饰器2
    3-4函数-全局变量
    3-5递归-函数
  • 原文地址:https://www.cnblogs.com/cbugs/p/9888091.html
Copyright © 2020-2023  润新知