Pandas快速入门学习

文章介绍了Python数据分析库Pandas中的两种主要数据结构——Series和DataFrame。Series是一维数据结构,可以看作是带索引的数组,而DataFrame是二维表格型数据结构,包含了行和列标签。文章展示了如何创建、操作这些数据结构,包括设置自定义索引、检索值、添加和删除列、以及进行行操作等。
摘要由CSDN通过智能技术生成

1.Series一维数据结构

#导入pandas、numpy  安装:pip3 install pandas   pip install numpy
import pandas as pd
import numpy as np
a = np.arange(5) #arange(5)指0~5范围,但不包含5,如[0 1 2 3 4]
b = pd.Series(a,index=['a','b','c','d','e']) #index自定义索引
# a    0
# b    1
# c    2
# d    3
# e    4
# dtype: int32
print(b[0]) #使用下标检查 值:0
print(b['a']) #使用索引检索 值:0

2.DataFrame二维数据结构

import pandas as pd
import numpy as np
a = np.arange(8).reshape(4,2) #arange(8)0~7 reshape(4,2)四行两列
#columns自定义列名  dtype自定义数据类型
b = pd.DataFrame(a,columns=['num','num01'],dtype=float,index=['a','b','c','d'])
print(b)
#    num  num01
# a  0.0    1.0
# b  2.0    3.0
# c  4.0    5.0
# d  6.0    7.0
import pandas as pd
import numpy as np
#字典
a = {'name':["zs",'lisi','ww'],'age':[23,44,43]}
b = pd.DataFrame(a,dtype=float) #DataFrame()二维矩阵 dtype定义数据类型  字典键被用作列名
#    name   age
# 0    zs  23.0
# 1  lisi  44.0
# 2    ww  43.0

data = [{'a':11,'c':43},{'a':33,'b':44,'c':66}]
b = pd.DataFrame(data,index=['a','b'],columns=['a','b'])
#     a     b
# a  11   NaN
# b  33  44.0

a = pd.Series([11,22,33],index=['a','b','c'])
b = pd.Series([33,44,55,77],index=['a','b','c','d'])
c = {'one':a,'two':b}
d = pd.DataFrame(c)
#     one  two
# a  11.0   33
# b  22.0   44
# c  33.0   55
# d   NaN   77
print(d['one']) 查询列
# a    11.0
# b    22.0
# c    33.0
# d     NaN
f = pd.Series([77,88,99,77],index=['a','b','c','d'])
d['three'] = f #新增列
#     one  two  three
# a  11.0   33     77
# b  22.0   44     88
# c  33.0   55     99
# d   NaN   77     77
del d['one'] #删除列 pop d['one']
#    two  three
# a   33     77
# b   44     88
# c   55     99
# d   77     77
g = d.loc['a'] #行查询
# two      33
# three    77
# Name: a, dtype: int64
h = d.iloc[0] #列查询
# a    77
# b    88
# c    99
# d    77
# dtype: int64
g = d[1:3] #切片行
#    two  three
# b   44     88
# c   55     99
o = pd.DataFrame([[66,66]],columns=['two','three'],index=['e'])
df = d.append(o) #添加行
#    two  three
# a   33     77
# b   44     88
# c   55     99
# d   77     77
# e   66     66
df = df.drop('a') #删除行
#    two  three
# b   44     88
# c   55     99
# d   77     77
# e   66     66

3.Panel三维数据结构

import pandas as pd
import numpy as np
a = np.random.randint(4,5,6)
p = pd.Panel(a)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MtoSlc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值