Many times when using Pandas dataframes, you are reading from a file to create one. There are cases, however, when you would like to create a dataframe from scratch. In this article, we will show you two ways to create a dataframe from scratch: using paralell lists and using a list of dictionaries.
The first way will create a dataframe is by using paralell lists. For example, let's say we have a list of sales people and the amount of sales they did. We can create a dataframe by adding this lists to a dictionary where the keys will become our column names. Then, we pass the dictionary to pd.DataFrame
.
import pandas as pd
people = ["Tom", "Laura", "Kathy"]
sales = [1000, 2000, 4000]
data = {
"person": people,
"sales": sales,
}
df = pd.DataFrame(data)
print(df.head())
We can create the same dataframe above using a list of dictionaries. The keys of each dictionary will be the column names.
import pandas as pd
data = [
{
"pearons": "Tom",
"sales": 1000,
},
{
"pearons": "Laura",
"sales": 2000,
},
{
"pearons": "Kathy",
"sales": 4000,
},
]
df = pd.DataFrame(data)
print(df.head())