标签:nump 跳过 直接 pandas 简单 重要 new 结构 数据结构
Series
import pandas as pd
from pandas import Series,DataFrame
obj = Series([4,7,-5,3])
# 索引在左边,值在右边,默认从0开始
obj
0 4
1 7
2 -5
3 3
dtype: int64
# 制定索引
obj2 = Series([4,7,-5,3],index = [‘a‘,‘b‘,‘c‘,‘d‘])
obj2
a 4
b 7
c -5
d 3
dtype: int64
# 查看索引
obj2.index
Index([‘a‘, ‘b‘, ‘c‘, ‘d‘], dtype=‘object‘)
# 查询
obj2[[‘a‘,‘b‘,‘c‘]]
a 4
b 7
c -5
dtype: int64
obj2[obj2>0]
a 4
b 7
d 3
dtype: int64
sdata = {‘ke‘:35000,‘text‘:70000,‘orgen‘:16000}
obj3 = Series(sdata)
obj3
ke 35000
text 70000
orgen 16000
dtype: int64
keys = [‘ke‘,‘text‘,‘orgen‘,‘xu‘]
obj4 = Series(sdata, index=keys)
obj4
ke 35000.0
text 70000.0
orgen 16000.0
xu NaN
dtype: float64
obj4[obj4.isnull()]
xu NaN
dtype: float64
obj4[obj4.notnull()]
ke 35000.0
text 70000.0
orgen 16000.0
dtype: float64
# 可以理解成对象名称
obj4.name = ‘pop‘
# 对象的索引的名称
obj4.index.name = ‘state‘
obj4
state
ke 35000.0
text 70000.0
orgen 16000.0
xu NaN
Name: pop, dtype: float64
# Series的索引可以就地修改
obj4.index = [‘new_ke‘,‘new_text‘,‘new_orgen‘,‘new_xu‘]
new_ke 35000.0
new_text 70000.0
new_orgen 16000.0
new_xu NaN
Name: pop, dtype: float64
data = {‘state‘:[‘oh‘,‘oh‘,‘vad‘,‘vad‘],
‘yead‘:[2000,2001,2002,2003],
‘pop‘:[1.5,1.7,3.6,2.4]
}
frame = DataFrame(data)
# 自动有序排列
yead state pop
0 2000 oh 1.5
1 2001 oh 1.7
2 2002 vad 3.6
3 2003 vad 2.4
# 如果传入的列在数据中找不到,就产生NaN
DataFrame(data,columns=[‘yar‘,‘yead‘])
yar yead
0 NaN 2000
1 NaN 2001
2 NaN 2002
3 NaN 2003
标签:nump 跳过 直接 pandas 简单 重要 new 结构 数据结构
原文地址:https://www.cnblogs.com/lishi-jie/p/9874102.html