0

I wrote a function. Dataframe was added 3 times using append. But the result is only added one last time.

====== ・It was an error to declare the dataframe type outside of the function first. So I declared it in a function.

・Later, I wrote Dataframe outside of def AddDataframe (ymd, sb, vol) :. Then I got an error. The error is below.

NameError: name 'Hisframe10' is not defined

import pandas as pd
def AddDataframe(ymd,sb,vol):
  data={'yyyymmdd':[],
      'Sell':[],
      'Buy':[],
      'Volume':[],        
      'JPX':[],
      'FutPrice':[]}
  Hisframe8=pd.DataFrame(data)
  Hisframe8
  print('')
  print('Hisframe8= ',Hisframe8)
  adddata={'yyyymmdd':[ymd],
    'Sell':[sb],
    'Buy':['Nan'],
    'Volume':[vol],           
    'JPX':[-1],
    'FutPrice':[0.]}
  Hisframe10=pd.DataFrame(adddata)
  Hisframe10
  return(Hisframe8.append(Hisframe10))
AddDataframe('2019-05-03','sell',123)
AddDataframe('2019-05-04','sell',345)
AddDataframe('2019-05-05','sell',456)
#Hisframe10  #err

======
I want to add 3 lines of data frame. How should I do it?

https://i.stack.imgur.com/yIWqN.jpg

saru999
  • 7
  • 4

2 Answers2

0

you can make Hisframe8 as global

import pandas as pd

Hisframe8=pd.DataFrame(columns=['yyyymmdd','Sell','Buy','Volume','JPX','FutPrice'])

def AddDataframe(ymd,sb,vol):
    global Hisframe8
    adddata={'yyyymmdd':[ymd],'Sell':[sb],'Buy':['Nan'],'Volume':[vol],'JPX':[-1],'FutPrice':[0.]}
    Hisframe10=pd.DataFrame(adddata)
    Hisframe8 = Hisframe8.append(Hisframe10)


AddDataframe('2019-05-03','sell',123)
AddDataframe('2019-05-04','sell',345)
AddDataframe('2019-05-05','sell',456)
print(Hisframe8)
Shijith
  • 4,602
  • 2
  • 20
  • 34
0

It is not necessary that you create an additional dataframe and append that to the first. You can instead just append the dictionary to the existing df as shown here: append dictionary to data frame

Also a better style maybe would be to define the dataframe you are inserting into first, since that is not the primary use of your function.

My suggestion:

structure = {'date':[], 'Sell':[], 'Buy':[], 'Volume':[], 'JPX':[], 'FutPrice':[]}
df = pd.DataFrame(structure)

def add_data(df, date, sb, vol):
   insert_dict = {'date':[date], 'Sell':[sb], 'Buy':[np.nan],
                  'Volume':[vol], 'JPX':[-1], 'FutPrice':[0.]}

   return df.append(insert_dict , ignore_index = True)

df_appended = add_data(df, '2019-01-01', 'sell', 456)

I hope this helps

fschlz
  • 1
  • 3
  • Thank you very much. However, if you add three lines as shown below, only the last line is included. Please let me know if you notice any points. – saru999 May 03 '19 at 14:45
  • import pandas as pd import numpy as np structure = {'date':[], 'Sell':[], 'Buy':[], 'Volume':[], 'JPX':[], 'FutPrice':[]} df = pd.DataFrame(structure) def add_data(df, date, sb, vol): insert_dict = {'date':[date], 'Sell':[sb], 'Buy':[np.nan],'Volume':[vol], 'JPX':[-1], 'FutPrice':[0.]} return(df.append(insert_dict , ignore_index = True)) df_appended = add_data(df, '2019-01-01', 'sell', 123) df_appended = add_data(df, '2019-01-02', 'sell', 345) df_appended = add_data(df, '2019-01-03', 'sell', 456) df_appended – saru999 May 03 '19 at 14:46
  • @saru999 you need to reference the newly created df in the function. if you want to add more lines to "df_appended" you need to call the function like this: > add_data(df_appended, '2019-01-01', 'sell', 123). Otherwise you only append to "df" which is actually not changed, since we create the new dataframe "df_appended". – fschlz May 03 '19 at 14:57