AppDividend
Latest Code Tutorials

Python Pandas DataFrame Tutorial | Data Structure In Pandas

0

Python Pandas DataFrame Tutorial | Data Structure Example In Pandas is today’s topic. Pandas library is the popular Python package for data science and machine learning, and with good reason: it offers dominant, expressive and flexible data structures that make the data manipulation and analysis effortless, among many other things. 

The DataFrame is one of these structures which helps us do the mathematical computation very easy. The Data frame is the two-dimensional data structure, for example, the data is aligned in the tabular fashion in rows and columns.

Python Pandas DataFrame Tutorial

DataFrame is two-dimensional size-mutable, potentially composite tabular data structure with labeled axes (rows and columns).

Firstly, the DataFrame can contain the following data type of data.

  1. The Pandas Series: a one-dimensional labeled array capable of holding any data type with axis labels or index. An example of a Series object is one column from a DataFrame.
  2. The NumPy ndarray, which can be a record or structure.
  3. The two-dimensional ndarray using NumPy.
  4. Dictionaries of one-dimensional ndarray’s, lists, dictionaries or Series.

How To Create a Pandas DataFrame

The Pandas DataFrame can be created using the following constructor.

pandas.DataFrame( data, index, columns, dtype, copy)

The data parameter takes forms like ndarray, series, map, lists, dict, constants and also another DataFrame.

For the row labels, the index parameter to be used for the resulting frame is an Optional Default np.arrange(n) if no index is passed to the function.

For column labels, the optional default syntax is: np.arrange(n). This is only true if no index is passed.

The dtype is Data type of each column.

The copy parameter is for copying of data if the default is False.

Create an Empty DataFrame

Empty DataFrame is basic DataFrame

Let see the following example.

# app.py

import pandas as pd
df1 = pd.DataFrame()
print(df1)

See the output below.

 

Python Pandas DataFrame Tutorial | Data Structure In Pandas

Create DataFrame from ndarrays

Let’s create a DataFrame from NumPy ndarrays.

# app.py

import pandas as pd
import numpy as np

data = np.array([18, 19, 21])
df1 = pd.DataFrame(data, index=[1, 2, 3])
print(df1)

In the above example, we have created a data from numpy ndarray and then pass it to the Dataframe function to construct the DataFrame.

 

Create DataFrame from ndarrays

Let’s add columns to construct the full table in DataFrame. See the below example.

# app.py

import pandas as pd
import numpy as np

data = np.array([['Game Of Thrones', 'HBO'], 
                ['Stranger Things', 'Netflix'],
                ['Casual', 'Hulu']])
df1 = pd.DataFrame(data, index=[1, 2, 3], columns=['Show Name', 'Streaming Service'])
print(df1)

Okay, so we have added the two columns name Show Name and Streaming Service. See the output below.

 

How To Create a Pandas DataFrame

Create a DataFrame from Dictionary

Let’s create a DataFrame using the dictionary.

# app.py

import pandas as pd
import numpy as np

data = {'Show Name': ['GameOfThrones', 'StrangerThings', 'Casual'], 
        'Streaming Service': ['HBO', 'Netflix', 'Hulu']}
df1 = pd.DataFrame(data)
print(df1)

See the below output.

 

Create a DataFrame from Dictionary

Create a DataFrame from Series

Let’s create a DataFrame from Series.

# app.py

import pandas as pd
import numpy as np

data = {'name' : 'krunal', 'website' : 'appdividend.com', 'role' : 'author'}
series = pd.Series(data)
df1 = pd.DataFrame(series)
print(df1)

See the below output.

 

Create a DataFrame from Series

Adding a Column to Your DataFrame

Let’s take an example where we can add the column to the dataframe.

# app.py

import pandas as pd
import numpy as np

data = {'age': [18, 19, 21], 'name': ['krunal', 'ankit', 'tejash']}
df1 = pd.DataFrame(data, index=[1, 2, 3])
print('Before column added')
print(df1)
df1['education'] = ['BE', 'MCA', 'MBA']
print('After column added')
print(df1)

In the above example, we have added one more column called education. See the output below.

 

Adding a Column to Your DataFrame

Removing a Column to Your DataFrame

See the following example where we have deleted one column from the DataFrame.

# app.py

import pandas as pd
import numpy as np

data = {'age': [18, 19, 21], 
        'name': ['krunal', 'ankit', 'tejash'],
        'education': ['BE', 'MCA', 'MBA']
        }
df1 = pd.DataFrame(data, index=[1, 2, 3])
print('After column deleted')
del df1['education']
print(df1)

In the above example, we have deleted the education column using the del function. See the below output.

 

Removing a Column to Your DataFrame

Adding a Row to Your DataFrame

We can add new rows to the DataFrame using an append function. Append function will append the rows at the end. Let’s see the following example.

# app.py

import pandas as pd
import numpy as np

data = {'age': [18, 19, 21], 
        'name': ['krunal', 'ankit', 'tejash'],
        'education': ['BE', 'MCA', 'MBA']
        }
df1 = pd.DataFrame(data, index=[1, 2, 3])
print('Before row added')
print(df1)

data2 = {'age': 22, 'name': 'rushabh', 'education': 'CA'}
df2 = pd.DataFrame(data2, index=[4])
print('After row added')
dfAdd = df1.append(df2)
print(dfAdd)

In the above example, we have defined df1 DataFrame and then defined df2 DataFrame.

Our goal is to add the row to the first DataFrame. For the added context, each data frame here works as a row. So we can add the DataFrame to other dataframe which is count as an add one row to another row.

So, to add the row, we need to add the DataFrame to another DataFrame.

The resulted DataFrame is the addition of both the DataFrames. In the above example, dfAdd is the final DataFrame which is the addition of both of the previous DataFrames. See the output below.

 

Adding a Row to Your DataFrame

Deleting a Row to Your DataFrame

We can delete the row using index label to delete or drop rows from a DataFrame. If the label is duplicated, then multiple rows will be dropped.

Let’s see the example where we drop the row using the index.

# app.py

import pandas as pd
import numpy as np

data = {'age': [18, 19, 21], 
        'name': ['krunal', 'ankit', 'tejash'],
        'education': ['BE', 'MCA', 'MBA']
        }
df1 = pd.DataFrame(data, index=[1, 2, 3])
print('Before row deleted')
print(df1)
print('After row deleted')
df2 = df1.drop(2)
print(df2)

In the above example, we are removing the row whose index is 2. See the output below.

 

Deleting a Row to Your DataFrame

Finally, Python Pandas DataFrame Tutorial | Data Structure In Pandas Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.