Series
Series创建一维数组
"""
Series是一种一维的数组型对象,它包含了一个值序列(与NumPy中的类型相似),并且包含了数据标签,称为索引(index)
"""
obj = pd.Series([1, 2, 3, 4, 5])
obj2 = pd.Series([4, 7, -7, 3], index=['a', 'b', 'c', 'e'])
Series数组读取, 更新数据
"""
Series 数据读取
"""
obj2 = pd.Series([4, 7, -7, 3], index=['a', 'b', 'c', 'e'])
values = obj2.values
index = obj2.index
result = obj2['a']
data = obj2[['a', 'c']]
obj2['a'] = 7
ob2[['a', 'b']] = 9
obj2[['a', 'b']] = [22, 10]
obj2.drop('a')
obj2.drop(['a', 'c'])
reindex 重建索引
"""
Series调用reindex方法时,会将数据按照新的索引进行排列,如果某个索引值之前并不存在,则会引入缺失值, 重建索引, 不会再原数组上进行改变, 而是生成新的数组
"""
import pandas as pd
obj = pd.Series([4.5, 4.3, 4.1, 0.5], index=['d', 'b', 'a', 'c'])
obj2 = obj.reindex(['a', 'b', 'c', 'd'])
"""
数组数据值进行填充: method可选参数允许我们使用诸如ffill等方法在重建索引时插值,ffill方法会将值前向填充
"""
obj3 = pd.Series(['BLUE', 'PURPLE', 'YELLOW'], index=[0, 2, 4])
obj4 = obj3.reindex(range(6), method='ffill')
print(obj4)
0 BLUE
1 BLUE
2 PURPLE
3 PURPLE
4 YELLOW
5 YELLOW
dtype: object
bj4 = obj3.reindex(range(6), fill_value='no')
Series数组进行算数运算
data = obj2 * 2
result = obj2[obj2 > 4 ]
result = 'b' in obj2
result_null = pd.isnull(obj2)
DataFrame
DataFrame创建一维数组
"""
DataFrame是二维的数组对象, 它既有行索引也有列索引, 尽管DataFrame是二维的,但你可以利用分层索引在DataFrame中展现更高维度的数据
"""
arr2 = pd.DataFrame({'state': ['ohi', 'ohi', 'nevada'], 'year': [2000, 2001, 2002], 'pop': [1.5, 2.9, 5.9]})
arr2 = pd.DataFrame({'state': ['ohi', 'ohi', 'nevada'], 'year': [2000, 2001, 2002], 'pop': [1.5, 2.9, 5.9]}, index=['a', 'b', 'c'], columns=['year', 'state', 'pop'])
DataFrame数据读取, 赋值, 删除某一列数据
data = arr2.head(2)
result = arr2['year']
value = arr2['year']['a']
_values = arr2.values
_columns = arr2.columns
_loc = arr2.loc['a']
arr2['debt'] = 19
del arr2['debt']
arr2.drop('year')
arr3 = pd.DataFrame([[1, 2, 3], [4, 9, 0]])
arr3[1]
arr[1][0]
DataFrame数组重建索引
"""
reindex可以改变行索引、列索引,也可以同时改变二者。当仅传入一个序列时,结果中的行会重建索引
"""
arr = pd.DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'b', 'c'], columns=['train', 'car', 'bus'])
print(arr)
arr2 = arr.reindex(['a', 'b', 'c', 'd'])
print(arr2)
arr3 = arr.reindex(columns=['train', 'car', 'plan'])
DataFrame数组进行算数运算