Pandas >> How to Create DataFrame in Pandas
Table of Contents
In this article, we will talk about different ways of creating DataFrame in Pandas
.
Give dict data in pandas.DataFrame constructor directly
import pandas as pd
df = pd.DataFrame({
"name": ["Kevin", "Jack", "Mary"],
"score": [80, 90, 95]
})
Give dict data outside pandas.DataFrame constructor
We can difine a dict data outside pandas.DataFrame constructor and specify the data by using data
option.
import pandas as pd
d = {
"name": ["Kevin", "Jack", "Mary"],
"score": [80, 90, 95]
}
df = pd.DataFrame(data=d)
Give a list of list data outside pandas.DataFrame constructor
We can difine a list of list data outside pandas.DataFrame constructor and specify the data by using data
and columns
option.
import pandas as pd
d = [["Kevin", 80], ["Jack", 90], ["Mary", 95]]
df2 = pd.DataFrame(data=d, columns=["name", "score"])
Result
name | score |
---|---|
Kevin | 80 |
Jack | 90 |
Mary | 95 |
Use pandas.DataFrame.from_records method
We can use pandas.DataFrame.from_records
to creates a DataFrame object from a structured ndarray, sequence of tuples or dicts, or DataFrame.
- structured ndarray
We can define a numpy array and use pandas.DataFrame.from_records
to create DataFrame.
import pandas as pd
data = np.array([('N001', 1), ('N002', 2), ('N003', 3)],
dtype=[('id', 'U1'), ('num', 'i4')])
df = pd.DataFrame.from_records(data)
- a list of dicts
We can define a list of dicts and use pandas.DataFrame.from_records
to create DataFrame.
import pandas as pd
data = [{'id': 1, 'name': 'a'},
{'id': 2, 'name': 'b'},
{'id': 3, 'name': 'c'},
{'id': 4, 'name': 'd'}]
df = pd.DataFrame.from_records(data)
- a list of tuples
We can define a list of tuples and use pandas.DataFrame.from_records
to create DataFrame.
import pandas as pd
data = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
df = pd.DataFrame.from_records(data, columns=["id", "name"])
Use pandas.DataFrame.from_dict method
We can use pandas.DataFrame.from_dict
to creates a DataFrame object from dictionary by columns or by index allowing dtype specification.
- the keys of dict become the DataFrame columns
We can define a dict and use pandas.DataFrame.from_dict
to create DataFrame, and its keys become DataFrame columns.
import pandas as pd
data = {'id': [1, 2, 3, 4], 'name': ['a', 'b', 'c', 'd']}
df = pd.DataFrame.from_dict(data)
Result
id | name | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |
3 | 4 | d |
- the keys of dict become the DataFrame rows
If we specify orient='index'
, the keys will become rows.
import pandas as pd
data = {'id': [1, 2, 3, 4], 'name': ['a', 'b', 'c', 'd']}
df = pd.DataFrame.from_dict(data, orient='index')
Result
0 | 1 | 2 | 3 | |
---|---|---|---|---|
id | 1 | 2 | 3 | 4 |
name | a | b | c | d |
- specify columns
When using the index
orientation, the column names can be specified manually.
We can define a list of tuples and use pandas.DataFrame.from_records
to create DataFrame.
import pandas as pd
data = {'id': [1, 2, 3, 4], 'name': ['a', 'b', 'c', 'd']}
df = pd.DataFrame.from_dict(data, orient='index', columns=['A', 'B', 'C', 'D'])
Result
A | B | C | D | |
---|---|---|---|---|
id | 1 | 2 | 3 | 4 |
name | a | b | c | d |