python 之pandas01

本文深入讲解了Pandas库中的核心数据类型Series和DataFrame的创建、索引、切片及数据类型转换等高级操作,通过实例展示了如何使用字典、列表创建一维和二维数组,并演示了索引和切片的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pandas常用数据类型有Series和DataFrame,Series为一维数组,DataFrame为二维,均为带有标签的数组。

Series有两种方法创建:通过列表或字典创建。

import pandas as pd
t = pd.Series([1,4,35,5,6])
print(t)
#取值大于5的索引和值
print(t[t>5])
#创建series时规定索引
t2 = pd.Series([1,4,35,5,6],index = list("abcde"))
print(t2,type(t2))
#改变数据类型
t3 = t2.astype("float")
print(t3)
0     1
1     4
2    35
3     5
4     6
dtype: int64
2    35
4     6
dtype: int64
a     1
b     4
c    35
d     5
e     6
dtype: int64 <class 'pandas.core.series.Series'>
a     1.0
b     4.0
c    35.0
d     5.0
e     6.0
#通过字典创建一维数组
temp_dict = {"name":"tom","age":30,"tel":"10086"}
t4 = pd.Series(temp_dict)
print(t4)
#引入string模块后,通过字典推导式创建字典,ascii_uppercase表示按顺序得出大写字母
a = {string.ascii_uppercase[i]:i for i in range(5)}
print(a)
b  = pd.Series(a)
print(b)
c = pd.Series(a,index = list(string.ascii_uppercase[4:8]))
print(c)
name      tom
age        30
tel     10086
dtype: object
{'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4}
A    0
B    1
C    2
D    3
E    4
dtype: int64
E    4.0
F    NaN
G    NaN
H    NaN
dtype: float64

注:在pandas中当出现nan时,pandas会自动将类型由int改为float,numpy无此操作。

Series切片

temp_dict = {"name":"tom","age":30,"tel":"10086"}
t = pd.Series(temp_dict)
print(t)
print("*"*10)
#通过索引查找
print(t["age"])
print(t[["age","tel"]])
print("*"*10)
#通过位置查找
print(t[0])
print(t[0:2])
name      tom
age        30
tel     10086
dtype: object
**********
30
age       30
tel    10086
dtype: object
**********
tom
name    tom
age      30
dtype: object

DataFrame
创建DataFrame
注:list为行索引,clolumns为列索引,表示索引要以列表形式,用小括号。

a = np.arange(12).reshape(3,4)
t = pd.DataFrame(a,index = list("abc"),columns = list("wxyz"))
print(t)

#生成字典
d1 = {"name":["tom","jack"],"age":[17,20],"tel":[10086,10010]}
print(d1)
#将字典转换为DataFrame
d1 = pd.DataFrame(d1)
print(d1)
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
{'name': ['tom', 'jack'], 'age': [17, 20], 'tel': [10086, 10010]}
   name  age    tel
0   tom   17  10086
1  jack   20  10010

在这里插入图片描述

索引
a.loc通过标签索引数据,a.iloc通过位置索引数据

t = pd.DataFrame(np.arange(12).reshape(3,4),index = list("abc"),columns = list("wxyz"))
print(t)
#通过标签索引数据
print(t.loc["a","z"])
#索引a这一行,或print(t.loc["a",:])
print(t.loc["a"])
#取多行
print(t.loc[["a","c"]])
#通过位置索引
print(t.iloc[1:])
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
3
w    0
x    1
y    2
z    3
Name: a, dtype: int32
   w  x   y   z
a  0  1   2   3
c  8  9  10  11
   w  x   y   z
b  4  5   6   7
c  8  9  10  11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值