import pandas as pd
df = pd.read_csv('telco_churn.csv')
#show top 10 and bottom 5 rows
df.head(10)
df.tail() # bottom 5 rows default
#create from a dictionary
temp_dict = {'col1': [1,2,3], 'col2': [4,5,6], 'col3': [7,8,9]}
dict_df = pd.DataFrame.from_dict(temp_dict)
#show columns and data type
df.columns
df.dtypes
#summary statistics
df.describe()
df.describe(include='object')
#filtering columns
df.State
df['International plan']
df[['State', 'International plan']]
df.State.unique()
#filtering on rows
df[()df['International plan'] == 'No']) & (df['Churn']==False)
#indexing with iloc
df.iloc[14]
df.iloc[14,0]
df.iloc[14,-1]
df.iloc[22:33]
#indexing with loc
state = df.copy()
state.set_index('State', inplace=True)
state.loc['OH']
#Update
#drop rows
df.isnull().sum()
df.dropna(inplace=True)
df.isnull().sum()
#drop columns
df.drop('Area code', axis=1)
#creating calculated columns
df['New Column'] = df['Total night minutes'] + df['Total intl minutes']
df.head()
#updating an entire column
df['New Column'] = 100
#updating a single value
df.iloc[0, -1] = 10
#condition based updating using apply
df['Churn Binary'] = df['Churn'].apply(lambda x: 1 if x == True else 0)
#ouput
df.to_csv('output.csv')
df.to_json()
df.to_html()
#delete DataFrame
del df