pandas笔记之series的使用

pandas包含两种数据类型:series和dataframe
series是一种一维数据结构,每一个元素都带有一个索引,与一维数组的含义相似,其中索引可以为数字或字符串。
dataframe是一种二维数据结构,数据以表格形式(与excel类似)存储,有对应的行和列

#通过列表数组字典构造series
import pandas as pd
import numpy as np

#列表和数组都是从0加上一列索引,而字典是key作为索引,value作为数据
mylist = list('abcedfghijklmnopqrstuvwxyz')   # 列表
myarr = np.arange(26)                         # 数组
mydict = dict(zip(mylist, myarr))             # 字典

# 构建方法
ser1 = pd.Series(mylist)
ser2 = pd.Series(myarr)
ser3 = pd.Series(mydict)
print(ser1)
print(ser2.head())  #输出前五行,head中没有数字的时候默认为5
print(ser3.head())

0 a
1 b
2 c
3 e
4 d
5 f
6 g
7 h
8 i
9 j
10 k
11 l
12 m
13 n
14 o
15 p
16 q
17 r
18 s
19 t
20 u
21 v
22 w
23 x
24 y
25 z
dtype: object
0 0
1 1
2 2
3 3
4 4
dtype: int32
a 0
b 1
c 2
e 3
d 4
dtype: int64

#把series转换为dataframe
df=ser3.to_frame()
#索引列转换为dataframe的列
df.reset_index(inplace=True)
print(df.head())

index 0
0 a 0
1 b 1
2 c 2
3 e 3
4 d 4

#结合多个series组成dataframe
#大括号中前面是列名,后面是数据值,而且两个series的索引值应该一样,如果索引值不一样,那么会对应的数据会显示NaN
#至于最左侧的索引还不知道是怎么确定的
ser4=pd.Series(np.arange(3))
df1=pd.DataFrame({'col1':ser1,'col2':ser4})   
df2=pd.DataFrame({'col1':ser3,'col2':ser4})   
print(df1.head())
print(df2.head())

col1 col2
0 a 0.0
1 b 1.0
2 c 2.0
3 e NaN
4 d NaN
col1 col2
0 NaN 0.0
1 NaN 1.0
2 NaN 2.0
a 0.0 NaN
b 1.0 NaN

#命名索引列名称
ser1.name='alphabets'
print(ser1.head())

0 a
1 b
2 c
3 e
4 d
Name: alphabets, dtype: object

ser5=pd.Series([1,2,3,4,5])
ser6=pd.Series([4,5,6,7,1])
#返回ser5中与ser6中不同元素的布尔型,不同的返回True,相同的返回False
ser7=~ser5.isin(ser6)
print(ser7)
#获取ser5中与ser6不同的的元素
ser5[ser7]

0 False
1 True
2 True
3 False
4 False
dtype: bool
1 2
2 3
dtype: int64

#获取series中单一项频率统计
#从0-7中随机挑取30个列表值
import random
ser_rand=pd.Series(np.take(['a','b','c','d','e','f','g','h'],np.random.randint(8,size=30)))
ser_rand.value_counts()

c 6
b 6
d 5
f 4
h 4
a 2
e 2
g 1
dtype: int64

#使numpy数组转化为给定形状的dataframe
ser8=pd.Series(np.random.randint(1,10,35))#随机抽取1-9,挑取35个
df=pd.DataFrame(ser8.values.reshape(7,5))#5行7列
print(df)

–0 1 2 3 4
0 7 9 6 7 1
1 6 9 7 2 3
2 1 3 1 9 7
3 3 5 1 6 3
4 1 9 9 6 8
5 8 3 3 8 3
6 6 5 6 4 4

#获取series中给定索引的元素
index=[0,4,2,7,19]
print(ser8[index])

0 7
4 1
2 6
7 7
19 3
dtype: int32
[0, 4, 2, 7, 19]

ser1=pd.Series(range(5))
ser2=pd.Series(list('abcde'))
#垂直拼接
df_vertical=pd.concat([ser1,ser2],axis=0)
print(df_vertical)
#水平拼接
df_horizontal=pd.concat([ser1,ser2],axis=1)
print(df_horizontal)

0 0
1 1
2 2
3 3
4 4
0 a
1 b
2 c
3 d
4 e
dtype: object
0 1
0 0 a
1 1 b
2 2 c
3 3 d
4 4 e

#计算series中每个元素的字符串的长度
ser3=pd.Series(['hello','are','you','OK'])
ser3.map(lambda x:len(x))

0 5
1 3
2 3
3 2
dtype: int64

#把一系列日期字符串转换为timeseries
ser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20','2021/01/28/18:15'])
pd.to_datetime(ser)

0 2010-01-01 00:00:00
1 2011-02-02 00:00:00
2 2012-03-03 00:00:00
3 2013-04-04 00:00:00
4 2014-05-05 00:00:00
5 2015-06-06 12:20:00
6 2021-01-28 18:15:00
dtype: datetime64[ns]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值