Pandas两种常用数据结构
- Series:系列(带索引的list),用于存储一行或一列数据及相关索引的集合。
- DataFrame:数据框
定义系列
from pandas import Series
x = Series(['a','b',3], index=['first','second','third'])
y = Series(['a','b',3,True])
y的默认索引为index=[0,1,2,3]
追加一个序列
n = Series(['4'])
# x.append(n)
x = x.append(n)
注:追加后的index默认为0
判断是否存在
'3' in x
>>>False
'3' in x.values
>>>True
切片
x[1:3]
>>>second b
third 3
dtype: object
定位获取(常用于随机抽样)
x[[0,2,1]]
>>>first a
third 3
second b
dtype: object
根据索引index删除
x.drop(0) #删除后保留,即显示操作后的结果,不进行实操作
>>>first a
second b
third 3
dtype: object
或:
x.drop('first')
x = x.drop(0) #删除x系列的原序列,不保留,进行实际操作
根据位置删除
x.drop(x.index[3])
根据值删除
x['a'!=x.values]
>>>second b
third 3
dtype: object
注:
x.values
>>>array(['a', 'b', 3], dtype=object)