35.pandas operations
### Create a dataframe
head()
unique()
count()
len( )
nunique()
value_counts()
sum()
applying the function
apply lamba functions
sort_values( )
isnull()
pivot_table()
import numpy as np
import pandas as pd
df = pd.DataFrame({'col1':[1,2,3,4],
'col2':[444,555,666,444],
'col3':['abc','def','ghi','xyz']})
print(df)
df.head() # first 5 rows
df.head(3)
df.tail(2)
df
df['col2'].unique()
df['col2'].count()
df.count()
df['col1'].nunique()
df['col2'].nunique()
df['col2'].value_counts()
len(df)
len(df['col1'])
df
df['col2'].sort_values()
df.isnull()
df['col1'].sum()
df['col1'].mean()
def times2(x):
return x*2
df['col1'].apply(times2)
df['col1'].apply(lambda x:x*2)
df.apply(len)
data = {'A':['foo','foo','foo','bar','bar','bar'],
'B':['one','one','two','two','one','one'],
'C':['x','y','x','y','x','y'],
'D':[1,3,2,5,4,1]}
df = pd.DataFrame(data)
df
df.pivot_table('D', index=['A','B'], columns=['C'])