Curriculum
Course: Data Science
Login

Curriculum

Data Science

Text lesson

DS DataFrame

Create a DataFrame with Pandas

A data frame is a structured format for representing data.

Let’s define a data frame with 3 columns and 5 rows, containing fictional numbers:

Example

import pandas as pd

d = {‘col1’: [12347], ‘col2’: [45695], ‘col3’: [7812111]}

df = pd.DataFrame(data=d)

print(df)

Example Explanation

  • Import the Pandas library as pd.
  • Define the data with columns and rows in a variable called d.
  • Create a data frame using the pd.DataFrame() function.
  • The data frame consists of 3 columns and 5 rows.
  • Display the data frame output using the print() function.

We use pd. before DataFrame() to indicate that we want to call the DataFrame() function from the Pandas library.

 

Note the capitalization of both the “D” and “F” in DataFrame!

Interpreting the Output

Here is the output:

img_dataframe_output

We can see that “col1”, “col2”, and “col3” are the column names.

The vertical numbers from 0 to 4 represent the row positions.

In Python, row numbering starts at zero.

Now, we can use Python to count the columns and rows.

To find the number of columns, we can use df.shape[1]:

Example

To count the number of columns:

count_column = df.shape[1]
print(count_column)

To find the number of rows, we can use df.shape[0]:

Example

To count the number of rows:

count_row = df.shape[0]
print(count_row)