How to create an empty dataframe

I saw this line of code from a book, what does that mean? After that, I print portfolio, it is just a underscore, but it is not empty DataFrame.

portfolio = portfolio = pd.DataFrame()

Answer

You’re seeing the result of how an empty DataFrame is displayed,. The DataFrame is in fact empty; you can see this better using a print or checking the empty attribute.

import pandas as pd
df = pd.DataFrame()

display(df)
#_

print(df)
#Empty DataFrame
#Columns: []
#Index: []

df.empty
#True

Leave a Reply

Your email address will not be published. Required fields are marked *