8. Study + Practice

import numpy as np
import pandas as pd

# Data check
s = pd.Series(np.random.rand(15))
print(s)

The default value is 5
print(s.head())  # to check the head
print(s.tail())  # see the tail

Reindex resorts according to the new index, and introduces the missing value NaN when the value does not exist
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)

Change the missing value to 0
s3 = s.reindex(['c'.'d'.'f'], fill_value=0)
print(s3)

# alignment
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
The missing values add up to NaN
print(s4 + s5)
# index doesn't have to be the same

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

s66 = s6.drop('n')
print(s66)
# The original data has not changed
print(s6)
print('-- -- -- -- -- -)
# The original data is changed
s7 = s6.drop('j', inplace=True)
print(s6) # delete data
print(s7) # None

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

# add
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)
Copy the code