Question #6960   Submitted by Answiki on 10/18/2022 at 06:55:14 PM UTC

How to create a DataFrame with Pandas in Python?

Answer   Submitted by Answiki on 01/11/2021 at 05:59:34 PM UTC

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

5 events in history
Question by Answiki 10/18/2022 at 06:55:14 PM
How to create a DataFrame with Pandas in Python?
Question by Answiki 01/11/2021 at 06:00:37 PM
How to build DataFrame with Pandas?
Answer by Answiki on 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 Answiki on 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 Answiki 01/11/2021 at 05:50:18 PM
How to create a DataFrame with Pandas?
# ID Query URL Count

Icons proudly provided by Friconix.