You can use DataFrame()
to create an empty Pandas dataframe. The following is the basic syntax as well as two examples.
import pandas as pd df = pd.DataFrame()
Example 1
The following creates an empty dataframe in Pandas and prints it out.
# import pandas package
import pandas as pd
# create an empty dataframe
df = pd.DataFrame()
# print it out
print(df)
The following is the output. As we can see, both columns and indexes are empty.
Empty DataFrame Columns: [] Index: []
Example 2
We can also first create an empty dataframe, and then add a column to this dataframe. The following is the Python code example.
Step 1: Create an empty dataframe
# import Pandas package
import pandas as pd
# create an empty dataframe
df = pd.DataFrame()
# print it out
print(df)
As expected and the same as Example 1, you will see that df has empty columns and indexes.
Empty DataFrame Columns: [] Index: []
Step 2: Add a column of data to the dataframe
# import Numpy package
import numpy as np
# generate a random array of number
array_1 = np.random.rand(5)
# create a column name 'numbers' and add the array of numbers into the column
df['numbers']=array_1
# print out the dataframe
print(df)
The following is the dataframe with a column called “numbers.” Thus, the code above changes the dataframe df from an empty dataframe to one with a column of data.
numbers 0 0.046959 1 0.229320 2 0.853977 3 0.226704 4 0.638635