Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty dataframe

Tags:

python

pandas

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()
like image 248
roudan Avatar asked Oct 17 '25 19:10

roudan


2 Answers

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
like image 73
ALollz Avatar answered Oct 20 '25 15:10

ALollz


portfolio = pd.DataFrame() does create an empty DataFrame where you can then add columns like e.g. portfolio['my_col'] = np.zeros(10) . I'm note sure I understand your question, could you give some more information?

like image 34
Adrien Wehrlé Avatar answered Oct 20 '25 16:10

Adrien Wehrlé