The simple datastructure pandas.DataFrame is described in this article. It includes the related information about the creation, index, addition and deletion. The text is very detailed.
In short: it’s a two-dimensional data structure (like table) with rows and columns.
Related course: Data Analysis with Python Pandas
Create DataFrame
What is a Pandas DataFrame
Pandas is a data manipulation module. DataFrame let you store tabular data in Python.
The DataFrame lets you easily store and manipulate tabular data like rows and columns.
A dataframe can be created from a list (see below), or a dictionary or numpy array (see bottom).
Create DataFrame from list
You can turn a single list into a pandas dataframe:
1 | import pandas as pd |
The contents of the dataframe is then:
1 | df |
Before the contents, you’ll see every element has an index (0,1,2).
This works for tables (n-dimensional arrays) too:
1 | import pandas as pd |
This outputs:
1 | df |
Related course: Data Analysis with Python Pandas
Columns
Select column
To select a column, you can use the column name.
Step 1: Create frame:
1 | 'Name','Age']) df = pd.DataFrame(data,columns=[ |
Step 2: Select by column name:
1 | 'Name'] df[ |
1 | 'Age'] df[ |
Column Addition
You can add a column to a dataframe. So this:
1 | df |
Becomes this:
1 | df |
Here’s how to do that:
Step 1: Create the dataframe
1 | 'Axel',32], ['Alice', 26], ['Alex', 45]] data = [[ |
Step 2: Create a new dataframe with column1
1,2,3], columns=['Example']) c = pd.DataFrame([
Step 3: Set the column name of your dataframe to that of the newly created one:1
2
3
4
5
6
7'Example'] = c['Example'] df[
df
Name Age Example
0 Axel 32 1
1 Alice 26 2
2 Alex 45 3
>>>
Column deletion
To delete a column, you can use the keyword del.
The original dataframe:
1 | df |
Then delete it:
1 | del df['Example'] |
And it will delete that column:
1 | df |
Related course: Data Analysis with Python Pandas
Rows
Select row
You can select a row using .loc[label].
1 | df |
You can select by index too, .iloc[index].
1 | 0] df.iloc[ |
Append row
You can append a row by calling the .append() method on the dataframe.
First create a new dataframe:
1 | 'Vivian',33]], columns= ['Name','Age']) user = pd.DataFrame([[ |
Then add it to the existing dataframe:
1 | df = df.append(user) |
1 | df |
Delete row
To delete a row, you can use the method .drop(index).
Start by creating a frame:1
2
3
4
5
6
7'Axel',32], ['Alice', 26], ['Alex', 45]] data = [[
'Name','Age']) df = pd.DataFrame(data,columns=[
df
Name Age
0 Axel 32
1 Alice 26
2 Alex 45
Lets delete the first row:
1 | 0) df = df.drop( |
DataFrame creation
Create DataFrame from dictionary
If you have a dictionary, you can turn it into a dataframe.
1 | import pandas as pd |
The keys in the dictionary are columns in the DataFrame, but there is no value for the index, so you need to set it yourself, and no default is to count from zero.
1 | 'first','second','third']) df = pd.DataFrame(d, index=[ |
Create DataFrame from array
An array (numpy array) can be converted into an dataframe too.
1 | import numpy as np |
Then turn it into a dataframe with the line:
1 | df = pd.DataFrame(ar) |
Creating a DataFrame assignment columns and index is created from a multi-dimensional array, otherwise it is the default, ugly.
1 | 'A','B','C'], columns=['One','Two','Three']) df = pd.DataFrame(ar, index=[ |
Create from DataFrame
You can copy parts of a dataframe into a new dataframe.
Using the dataframe above:
1 | 'One','Two']].copy() df2 = df[[ |
Create from CSV
If you have a csv file (Google Sheets can save as csv), you can load it like this:
1 | # Import pandas as pd |