Call Us : 0674-2361252   

Python Pandas Tutorial : Learn Pandas for Data Science

What is Python Pandas?

Python pandas is a software library specifically developed for data manipulation and analysis. Pandas is free software released under the three clause BSD license.Pandas provides two data structures like series and data frameand operations for manipulating numerical table and time series.

Python pandas is well suited for different kinds of data, such as:

  • Tabular data with heterogeneously-typed columns
  • Ordered and unordered time series data
  • Arbitrary matrix data with row & column labels
  • Unlabelled data
  • Any other form of observational or statistical data sets
  • How to install Pandas?

    To install Python Pandas, go to your command line/ terminal and type “pip install pandas” or else, if you have anaconda installed in your system, just type in “conda install pandas”. Once the installation is completed, go to your IDE (here I have used Jupyter Notebook) and simply import pandas library by typing:

    1. import pandas as pd
    2. print(pd.__version__)
    3. Output: 0.24.2

    Pandas Series :
    Pandas Series is a data structure(one-dimensional labeled array) having capability to contain any type of data item like int, string, float, python objects etc.

    • #Pandas Series example-1
    • x=[10.20,True,5.2,'python']
    • s1=pd.Series(x)
    • s1

    Output:
    0    10.2
    1    True
    2    5.2
    3    python
    dtype:    object

    order=['a','b','c','d'] s1=pd.Series(x,index=order)
    s1
    Output:
    a     10.2
    b     True
    c     5.2
    d     python
    dtype:     object


    #Example-2
    importnumpy as np
    y=np.random.randn(10)
    s2=pd.Series(y)
    s2

    Output:
    0     0.217865
    1     -1.330115
    2     -1.173223
    3     -2.092255
    4     -0.845571
    5     -0.029044
    6     0.650718
    7     0.852031
    8     -0.181152
    9     0.863597
    dtype:      float64

    #Example-3
    x3=[7,4,9,3]
    s3=pd.Series(x3)
    s3

    Output:
    0     7
    1     4
    2     9
    3     3
    dtype:     int64

    s3.mean()
    5.75
    s3.median()
    5.5
    s3.min()
    3
    s3.max()
    9

    #Example-4
    z=[1,2,3,4]
    s4=pd.Series(z)
    s4

    Output:
    0            1
    1            2
    2            3
    3            4
    dtype:      int64

    s3.add(s4)
    Output:
    0            8
    1            6
    2            12
    3            7
    dtype:           int64

    s3.div(s4)
    Output:
    0            7.00
    1            2.00
    2            3.00
    3            0.75
    dtype:            float64

    #Series creation by taking a dictionary
    z1={'a':5,'b':6,'c':7}
    s5=pd.Series(z1)
    s5
    Output:
    a            5
    b            6
    c            7
    dtype:            int64
    #Create series from tuple
    x4=(4,5,6)
    s6=pd.Series(x4)
    s6

    Output:
    0            4
    1            5
    2            6
    dtype:            int64
    Python pandas data frame Tutorial

    import pandas as pd

    # Create a data frame from a list

    l1 = ['python','java','ruby','php','devoops','python']

    # Calling DataFrame constructor on list

    df = pd.DataFrame(l1)

    print(df)

    output:
    0            python
    1            java
    2            ruby
    3            php
    4            devoops
    5            python

    In  [2]; #Create a data frame from a List
    l1  =  ['python'.'java','ruby','php','devoops','python']

    #    Calling Dataframe constructor on list
    df   =  pd.DataFrame (l1)
    print(df)


    0            python
    1            java
    2            ruby
    3            php
    4            devoops
    5            python
    #Create a data frame from a dictionary
    data = {'eid':[101,102,103,104,105],'ename':['Suvransu','Lipu','Ashok','Surya','Judisthira']}
    # Create DataFrame
    df1 = pd.DataFrame(data)
    # Print the output.
    print(df1)

    output:
    eidename
    0       101Suvransu
    1       102Lipu
    2       103       Ashok
    3       104       Surya
    4       105Judisthira

    #To access a particular column values
    print(df1['ename'])
    output:
    0       Suvransu
    1       Lipu
    2       Ashok
    3       Surya
    4       Judisthira
    Name: ename, dtype: object

    #Create a data frame from a csv file
    d=pd.read_csv("E:\emp1.csv")
    df2=pd.DataFrame(d)
    print(df2)

    output: eidenameesalary
    0       101Akash       10000
    1       102       Rahul       20000
    2       103Santosh       30000
    3       104Sachin       40000
    4       105       Ajay       50000

    # Concatenating two data frames
    # Define a dictionary containing employee data
    data1 = {'Name':['Ashok', 'Lipu', 'Surya', 'Suvransu'],
    'Age':[28, 25, 23, 33],
    'Address':['BBSR', 'Katak', 'Baripada', 'Balasore'],
    'Qualification':['BTech', 'MCA', 'MBA', 'BCA']}

    # Define a dictionary containing employee data
    data2 = {'Name':['Akshay', 'Ajay', 'Akash', 'Sanghamitra'],
    'Age':[37, 34, 22, 32],
    'Address':['Paradeep', 'Kujang', 'Jajpur', 'Chatua'],
    'Qualification':['Mtech', 'BCA', 'MCA', 'Bsc']}

    # Convert the dictionary into DataFrame
    df3 = pd.DataFrame(data1,index=[0, 1, 2, 3])

    # Convert the dictionary into DataFrame
    df4 = pd.DataFrame(data2, index=[4, 5, 6, 7])

    print(df3, "\n\n", df4)

    frames = [df3, df4]

    result = pd.concat(frames)
    print(result)

    #Merging two data frames
    # Define a dictionary containing employee data
    data1 = {'id': [100, 101, 102, 103],
    'Name':['Suvransu', 'Lipu', 'Ashok', 'Surya'],
    'Age':[29, 35, 32, 34],}

    # Define a dictionary containing employee data
    data2 = {'id': [100, 101, 102, 103],
    'Address':['BBSR', 'Katak', 'Balasore', 'Paradeep'],
    'Qualification':['Mtech', 'BCA', 'Bsc', 'B.tech']}

    # Convert the dictionary into DataFrame
    df5 = pd.DataFrame(data1)

    # Convert the dictionary into DataFrame
    df6 = pd.DataFrame(data2)
    print(df5, "\n\n", df6)

    print(m)

    © Copyright 2019 Silan Technology.
    Designed by Silan Technology