pandas Series基本技巧

381 阅读1分钟

8. 学习 + 练习

import numpy as np
import pandas as pd

# 数据查看
s = pd.Series(np.random.rand(15))
print(s)

# 默认5条数据
print(s.head())  # 查看头
print(s.tail())  # 查看尾

# 重新索引 reindex    根据新索引重新排序,当值不存在当时候  引入缺失值 NaN
s = pd.Series(np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'])
print(s)

s1 = s.reindex(['c', 'd', 'a', 'b', 'e', 'f'])
print(s1)

s2 = s.reindex(['c', 'd', 'f'])
print(s2)

# 将缺失值 修改为0
s3 = s.reindex(['c', 'd', 'f'], fill_value=0)
print(s3)

# 对齐
s4 = pd.Series(np.random.rand(3), index=['jack', 'marry', 'cindy'])
# s4 = pd.Series(np.random.rand(3), index=['jack', 'jack', 'marry', 'cindy'])
s5 = pd.Series(np.random.rand(3), index=['wang', 'jack', 'marry'])
print(s4)
print(s5)

'''
cindy         NaN
jack     0.762138
marry    1.406145
wang          NaN
dtype: float64
'''
# 缺失值相加当时候就是NaN
print(s4 + s5)
# index 不一定非要一样

# 删除  drop
s6 = pd.Series(np.random.rand(5), index=list('ngijx'))
print(s)

s66 = s6.drop('n')
print(s66)
# 原数据没有发生改变
print(s6)
print('------')
# 原数据发生改变
s7 = s6.drop('j', inplace=True)
print(s6) # 删掉数据
print(s7) # None

s62 = s6.drop(['n', 'i'])
print(s62)

# 添加
s8 = pd.Series(np.random.rand(5))
s9 = pd.Series(np.random.rand(5), index=list('ngijx'))
print(s8)
print(s9)
s8[5] = 100
s9['x'] = 100
print(s8)
print(s9)
print('------')

s10 = s8.append(s9)
print(s10)