How to Create a Pandas DataFrame
Let’s see the many different ways we can create a Pandas DataFrame.
Try testing these small examples in the Programiz Python Online Compiler. If you plan to use
pandas
, remember to runimport pandas as pd
.
1. Create an empty DataFrame
df = pd.DataFrame()
2. Create DataFrame with one column
Let’s create a DataFrame with a single column named col
with values 0
, 1
, and 2
.
2.1. From a list
data = [0, 1, 2]
df = pd.DataFrame(data, columns=['col'])
2.2. From a Series
data = pd.Series([0, 1, 2])
df = pd.DataFrame(data, columns=['col'])
3. Create DataFrame with multiple columns
Let’s create a DataFrame with columns named col1
and col2
.
col1
should have values 0
, 1
, 2
.
col2
should have values 3
, 4
, 5
.
3.1. From a list of lists
data = [
[0, 3],
[1, 4],
[2, 5]
]
df = pd.DataFrame(data, columns=['col1', 'col2'])
For all of the methods below, we can explicitly add index labels to our DataFrame using the index
parameter.
df = pd.DataFrame(data, index=['index1', 'index2', 'index3'])
3.2. From a dict of lists
data = {
'col1': [0, 1, 2],
'col2': [3, 4, 5]
}
df = pd.DataFrame(data)
3.3. From a list of dicts
data = [
{'col1': 0, 'col2': 3},
{'col1': 1, 'col2': 4},
{'col1': 2, 'col2': 5}
]
df = pd.DataFrame(data)
3.4. From a zip
of lists
col1 = [0, 1, 2]
col2 = [3, 4, 5]
list_of_tuples = list(zip(col1, col2))
df = pd.DataFrame(list_of_tuples, columns=['col1', 'col2'])
3.5. From a dict of Series
data = {
'col1': pd.Series([0, 1, 2]),
'col2': pd.Series([3, 4, 5])
}
df = pd.DataFrame(data)
To add index labels with this method, we should apply them to the Series objects themselves.
data = {
'col1': pd.Series([0, 1, 2], index=['index1', 'index2', 'index3']),
'col2': pd.Series([3, 4, 5], index=['index1', 'index2', 'index3'])
}
df = pd.DataFrame(data)