목록Pandas (57)
Note
How to get the items not common to both series A and series B? # Input ser1 = pd.Series([1, 2, 3, 4, 5]) ser2 = pd.Series([4, 5, 6, 7, 8]) # Solution ser_u = pd.Series(np.union1d(ser1, ser2)) # union ser_i = pd.Series(np.intersect1d(ser1, ser2)) # intersect ser_u[~ser_u.isin(ser_i)]
How to get the items of series A not present in series B? # Input ser1 = pd.Series([1, 2, 3, 4, 5]) ser2 = pd.Series([4, 5, 6, 7, 8]) # Solution ser1[~ser1.isin(ser2)]
How to assign name to the series’ index? # Input ser = pd.Series(list('abcedfghijklmnopqrstuvwxyz')) # Solution ser.name = 'alphabets' ser.head()
How to combine many series to form a dataframe? # Input import numpy as np ser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz')) ser2 = pd.Series(np.arange(26)) # Solution 1 df = pd.concat([ser1, ser2], axis=1) # Solution 2 df = pd.DataFrame({'col1': ser1, 'col2': ser2})