Start with Python and Pandas – Part 2
Hello Friends! I’ll be continuing from previous article about Python and we’ll learn more about Python.
Input is the dataframe
Get values of any column
input['Type'][0:6]
#it will return top 6 records of column Type
Get number of non-null records in each column
input.count()
Sort data based on some column
input.sort_values('Age',ascending=True) or
input.sort_values('Age',ascending=1)
#It will sort data in ascending order based on column Age
Create new column
input['Survived_2']= input['Survived']
#it will create new column Survived_2
Delete column
del input['Survived_2'] # del is the syntax for deletion it will delete new column Survived_2
Filter Data
input_male = input[input.Sex=='male']
#Filter for males using column Sex
input_male = input[input['Sex']=='male']
#Filter for males using column Sex
Grouping and aggregation based on some column
input_survived= input.groupby(['Survived']).size()
# it will return the count of values for different survived values
input_survived= input.groupby(['Survived', 'Sex']).size()
# it will return the count of values for different survived and sex values
Joining in Python using Pandas
New_Df = pd.merge(df1, df2, left_on='key1', right_on='key2', how='left'/'right'/'inner'/'outer')
df1 = 1st data frame
df2 = 2nd data frame
left_on = key on which join
right_on = key from 2nd data frame
how = what kind of join is required left, right or other
Keep visiting Analytics Tuts for more tutorials.
Thanks for reading! Comment your suggestions and queries
Pingback: Start with Python and Pandas – Part 3 - Analytics Tuts