0

Hi I created an empty dataframe, when I added new value to the dataframe, the new column is created, but the new value is not added, so the dataframe is still empty.

unrealized_amt_df=pd.DataFrame()
unrealized_amt_df['Name']='abc'

The shape of the dataframe becomes (0,1). When I run the following:

unrealized_amt_df['Date']=datetime.today().strftime('%Y-%m-%d')

The shape becomes (0,2) now.The columns are 'Name','Date'

Does anyone know how to fix it? Thank you very much in advance.

1 Answer 1

1

I think better is create list and then Dataframe:

L = ['abc', datetime.today().strftime('%Y-%m-%d')]
unrealized_amt_df=pd.DataFrame([L], columns=['Name','Date'])

print (unrealized_amt_df)
  Name        Date
0  abc  2020-04-29

But is it possible with specified new index value, here 0 in DataFrame.loc, but slow, so not recommended:

unrealized_amt_df=pd.DataFrame()
unrealized_amt_df.loc[0, 'Name']='abc'
unrealized_amt_df.loc[0, 'Date']=datetime.today().strftime('%Y-%m-%d')
print (unrealized_amt_df)
  Name        Date
0  abc  2020-04-29
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.