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.

liga810
  • 2,824
  • 2
  • 12
  • 11

1 Answers1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252