Home > Blockchain >  not able to append data in dataframe
not able to append data in dataframe

Time:06-17

we are trying to create new data frame using function and then append them in final data frame. i tried below code but not able to get appended data frame in final result. can anyone guide me here.

import pandas as pd
def my_fun(var):
    import pandas as pd
    temp={"File_Exist":'no',"desc":var,"Bucket":'bucket',"name":'fadsf',"size":'','last_modified':'',"generation":''}
    temp_df=pd.DataFrame(temp,columns=list(temp.keys()), index=['x'])

    return temp_df

final_df=pd.DataFrame()   
data_df=my_fun('my 1st file')
final_df.append(data_df,ignore_index = True)
data_df=my_fun('my 2nd file')
final_df.append(data_df,ignore_index = True)

print(final_df) 

CodePudding user response:

As of pandas v1.4.0, append is deprecated in favor of concat (source). Also, you must assign the return value to final_df in order to see changes; append and concat do not update the data frames for you.

import pandas as pd
def my_fun(var):
    temp={"File_Exist":'no',"desc":var,"Bucket":'bucket',"name":'fadsf',"size":'','last_modified':'',"generation":''}
    temp_df=pd.DataFrame(temp,columns=list(temp.keys()), index=['x'])

    return temp_df

final_df = pd.DataFrame()   

final_df = pd.concat([final_df, my_fun('my 1st file')],ignore_index = True)
final_df = pd.concat([final_df, my_fun('my 2nd file')],ignore_index = True)

print(final_df) 

CodePudding user response:

You are appending but not assigning the appended dataframe to any variable - you need to re-assign the appended dataframe to final_df

final_df = pd.DataFrame()   
data_df = my_fun('my 1st file')
final_df = final_df.append(data_df,ignore_index = True)
data_df = my_fun('my 2nd file')
final_df = final_df.append(data_df,ignore_index = True)
print(final_df)

CodePudding user response:

pd.concat([my_fun('my 1st file'), my_fun('my 2nd file')])
  • Related