zoukankan      html  css  js  c++  java
  • Python的DataFrame基础使用

    1.使用DataFrame建表的三种方式

    import numpy as np
    import pandas as pd
    
    test_1 = pd.DataFrame(np.random.rand(4, 4),
                          index=list('ABCD'), columns=list('1234'))  # 产生随机数,index行,columns列
    test_2 = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],
                          index=list('1234'), columns=list('ABCD'))  # 自己输入
    dic1 = {'name': ['小明', '小红', '狗蛋', '铁柱'],
            'age': [17, 20, 5, 40], 'sex': ['', '', '', '']}  # 使用字典进行输入
    test_3 = pd.DataFrame(dic1, index=list('ABCD'))
    print(test_1, '
    ')
    print(test_2, '
    ')
    print(test_3, '
    ')

    2.查看数据情况

    print('查看数据类型:
    ', test_3.dtypes, '
    ')
    print('看前两行:
    ', test_3.head(2), '
    ')
    print('看后两行:
    ', test_3.tail(2), '
    ')
    print('index看行名:
    ', test_3.index, '
    ')
    print('columns看行名:
    ', test_3.columns, '
    ')

    3.数据检索

    print('看所有数据值:
    ', test_3.values, '
    ')
    print('查看name列的数据:
    ', test_3['name'].values, '
    ')
    print('使用loc进行行检索:
    ', test_3.loc['A'], '
    ')
    print('使用iloc进行行检索:
    ', test_3.iloc[0], '
    ')
    print('直接使用名字进行列检索,但不适合行检索:
    ', test_3['name'], '
    ')

    4.对表进行描述

    print('对表进行描述:
    ', test_3.describe(), '
    ')

    5.表的合并

    print('进行转置:
    ', test_3.T, '
    ')
    print('查看行数:', test_3.shape[0], '查看列数:', test_3.shape[1], '
    ')
    # print('对表进行描述:
    ', test_3.describe(), '
    ')
    
    test_3.insert(3, 'skin', ['b', 'w', 'w', 'y'])
    print('对表用insert进行插入:
    ', test_3, '
    ')
    
    test_4 = pd.DataFrame(['T', 'E', 'W', 'A'], index=list('ABCD'), columns=list('N'))
    # print('新建的DataFrame:
    ', test_4, '
    ')
    print('合并DataFrame:
    ', test_3.join(test_4), '
    ')

  • 相关阅读:
    CSS
    iframe自适应宽度和高度
    用css实现文字超出指定宽度显示省略号(...)
    删除数据时的提示效果(遮罩)
    网页选项卡的应用
    列表应用(导航菜单)
    下拉列表框中的事件应用(联动展示数据)
    jQuery移除事件
    toggle()方法
    jQuery切换事件
  • 原文地址:https://www.cnblogs.com/Wind-Flies/p/11636953.html
Copyright © 2011-2022 走看看