{
    "id": "pandas_cheat_sheet",
    "name": "Pandas",
    "description": "Explanation of Pandas library with syntax and properties",
    "metadata": {
        "sourceName": "Analytics Vidhya",
        "sourceUrl": "http://www.analyticsvidhya.com/blog/2015/07/11-steps-perform-data-analysis-pandas-python/"
    },
    "aliases": [
        "python pandas"
    ],
    "template_type": "terminal",
    "section_order": [
        "Reading and Writing Methods",
        "Getting Preview of Dataframe",
        "Rename Columns of Dataframe",
        "Selecting Columns and Rows",
        "Handling Missing Values",
        "Creating New Columns",
        "Aggregate",
        "Merging/Concatinating Dataframes",
        "Applying Functions to Dataframes",
        "Identify Unique Values",
        "Basic Statical Methods"
    ],
    "sections": {
        "Reading and Writing Methods": [{
            "val": "Reading a CSV file",
            "key": "df=pd.read_csv('Anyname.csv')"  
        }, {             
            "val": "Writing content of dataframe to CSV file",
            "key": "df.to_csv(Newfile.csv)"
        }, {
            "val": "Reading an Excel file",
            "key": "df=pd.read_excel('File_Name.xlsx','sheet1')"
        }, {
            "val": "Writing content of dataframe to Excel file",
            "key": "df.to_excel('Abc.xlsx',sheet_name='sheet2')"  
        }],
        "Getting Preview of Dataframe": [{
            "val": "Looking at top n records",
            "key": "df.head(n)"
        }, {
            "val": "Looking at bottom n records",
            "key": "df.tail(n)"
        }, {
            "val": "View columns name",
            "key": "df.columns"
        }],
        "Rename Columns of Dataframe": [{
            "val": "Create a new dataframe",
            "key": "df2=df.rename(columns={'old_columnname':'new_columnname'})"
        }, {
            "val": "To rename the column of existing dataframe",
            "key": "df.rename(columns={'old_columnname':'new_columnname',inplace=True}"         
        }],
        "Selecting Columns and Rows": [{
            "val": "Accessing sub dataframes",
            "key": "df[['column1','column2']]"
        }, {
            "val": "Filtering Records",
            "key": "df[df['column1']>10]"
        }, {
            "val": "Filtering Records using &(and)",
            "key": "df\\[(df\\['column1'\\]>10) & df\\['column2'\\]==50\\]"
        }, {
            "val": "Filtering Records using |(or)",
            "key": "df\\[(df\\['column1'\\]>10) | df\\['column2'\\]==50\\]"
        }],
        "Handling Missing Values": [{
            "val": "Drop rows and columns having missing data",
            "key": "df1.dropna()"
        }, {
            "val": "Replace all missing values with 5",
            "key": "df2.filna(value=5)"
        }, {
            "val": "Find mean value of column",
            "key": "mean=df2['column1'].mean()"
        }, {
            "val": "Replace all the missing values of column1 with mean of available values",
            "key": "df2['column1'].filna(mean)"
        }],
        "Creating New Columns": [{
            "val": "Create a copy of existing column2",
            "key": "df['NewColumn1']=df['column2']"
        }, {
            "val": "Add 10 to existing column2 then create new column",
            "key": "df['NewColumn2']=df['column2']+10"
        }, {
            "val": "Add elements of column1 and column2 then create new column",
            "key": "df['NewColumn3']=df['column1']+df['column2']"
        }],
        "Aggregate": [{
            "val": "Spliting the data into groups and apply fucntions to each groups indivdually",
            "key": "[df.groupby('column1').sum()] [df.groupby(\\['column1','column2'\\]).count()]"
        }, {
            "val": "Pivot table helps to genrate data structure. it has three components index, columns and values",
            "key": "pd.pivot_table(df,values='column1',index=['column2','column3'],columns=['column4'])"
        }, {
            "val": "To find count in column in Pivot table",
            "key": "pd.pivot_table(df,values='column1',index=['column2','column3'],columns=['column4'],aggfunc=len"
        }, {
            "val": "Crosstab computes the simple cross tabulation of two factors",
            "key": "pd.crosstab(df.column1,df.column2)"
        }],
        "Merging/Concatinating Dataframes": [{
            "val": "Concatinate two or more dataframes based on columns",
            "key": "pd.concat([df1,df2])"
        }, {
            "val": "We can perform right, left and inner joins also",
            "key": "[pd.merge(df1,df2,on='column1',how='inner')] [pd.merge(df1,df2,on='column1',how='left')] [pd.merge(df1,df2,on='column1',how='right')]"
        }],
        "Applying Functions to Dataframes": [{
            "val": "Map: This will add 10 to each element of column1",
            "key": "df\\['column1'\\].map(lambda x:10+x)"
        }, {
            "val": "Concatinate 'hello' at the beginning of each elements",
            "key": "df\\['column1'\\].map(lambda x:'hello'+x)"
        }, {
            "val": "Apply: Applies a fucntion along any axis of the dataframe",
            "key": "df[['column1','column2']].apply(sum)"
        }],
        "Identify Unique Values": [{
            "val": "Returns unique values of a column",
            "key": "df['column1'].unique()"
        }],
        "Basic Statical Methods": [{
            "val": "Returns a quick stats(count, mean, std, min, first quartile) on suitable columns",
            "key": "df.describe()"
        }, {
            "val": "Returns co-variance between suitable columns",
            "key": "df.cov()"
        }, {
            "val": "Returns co-relation between suitable columns",
            "key": "df.corr()"
        }]
    }
}
