👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.
How to create a DataFrame with Pandas?
In Python, there are several ways to create a DataFrame with Pandas. In the following, we assume Pandas is imported according to this line:
import pandas as pd
Create an empty DataFrame
>>> df = pd.DataFrame()
Empty DataFrame
Columns: []
Index: []
Create DataFrame from lists
>>> list = [[1,2], [3,4], [4,5]]
>>> df = pd.DataFrame(list)
0 1
0 1 2
1 3 4
2 4 5
Create DataFrame from dictionaries
>>> dict={'C1': [1, 4], 'C2': [2, 5], 'C3': [3, 6] }
>>> df = pd.DataFrame(dict)
C1 C2 C3
0 1 2 3
1 4 5 6
Create DataFrame from NumPy arrays
>>> import numpy as np
>>> array =np.array([ [1,2,3] , [4,5,6] ])
>>> df = pd.DataFrame(array)
0 1 2
0 1 2 3
1 4 5 6