What is Dataframe in Python?

This tutorial explains what a dataframe is in Python. Further, I will show how to define a dataframe using Pandas with examples. Finally, I will show how to define a dataframe from Pandas.series.

Introduction of Dataframe

Dataframe is two-dimensional, size-mutable, potentially heterogeneous tabular data. For instance, below is a dataframe of Microsoft stock price. (Regarding how to download it, you can refer to here. )

Often, we can construct a dataframe from a dictionary and the following is the example in Python.

import pandas as pd
d = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
print(type(d))
print(d)
df = pd.DataFrame(data=d)
print(df)
<class 'dict'>
{'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
   Brand  Count
0  Tesla      3
1   Ford      4

Pandas.Series

d = {'a': 1, 'b': 2, 'c': 3}
ser = pd.Series(data=d, index=['b', 'c', 'a'])
print(ser)
b    2
c    3
a    1

The following code shows how to constructe a DataFrame from a dictionary including series.

import pandas as pd
d = {'col_1': [0, 1, 2, 3], 'col_2': pd.Series([3, 4], index=[0, 3])}

print(pd.DataFrame(data=d,index=[0, 1, 2, 3]))
   col_1  col_2
0      0    3.0
1      1    NaN
2      2    NaN
3      3    4.0