Question #633

How to create a DataFrame with Pandas?

Answer

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

4 events in history
Question by Alphonsio 01/11/2021 at 06:00:37 PM
How to build DataFrame with Pandas?
Answer by Alphonsio 01/11/2021 at 05:59:34 PM

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

Answer by Alphonsio 01/11/2021 at 05:59:02 PM

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 a 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

Question by Alphonsio 01/11/2021 at 05:50:18 PM
How to create a DataFrame with Pandas?
# ID Query URL Count

Icons proudly provided by Friconix.