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, '\n')
print(test_2, '\n')
print(test_3, '\n')

2.查看数据情况

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

3.数据检索

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

4.对表进行描述

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

5.表的合并

print('进行转置:\n', test_3.T, '\n')
print('查看行数:', test_3.shape[0], '查看列数:', test_3.shape[1], '\n')
# print('对表进行描述:\n', test_3.describe(), '\n')

test_3.insert(3, 'skin', ['b', 'w', 'w', 'y'])
print('对表用insert进行插入:\n', test_3, '\n')

test_4 = pd.DataFrame(['T', 'E', 'W', 'A'], index=list('ABCD'), columns=list('N'))
# print('新建的DataFrame:\n', test_4, '\n')
print('合并DataFrame:\n', test_3.join(test_4), '\n')


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM