'Good alternative to Pandas .append() method, now that it is being deprecated?
I use the following method a lot to append a single row to a dataframe. One thing I really like about it is that it allows you to append a simple dict object. For example:
# Creating an empty dataframe
df = pd.DataFrame(columns=['a', 'b'])
# Appending a row
df = df.append({ 'a': 1, 'b': 2 }, ignore_index=True)
Again, what I like most about this is that the code is very clean and requires very few lines. Now I suppose the recommended alternative is:
# Create the new row as its own dataframe
df_new_row = pd.DataFrame({ 'a': [1], 'b': [2] })
df = pd.concat([df, df_new_row])
So what was one line of code before is now two lines with a throwaway variable and extra cruft where I create the new dataframe. :( Is there a good way to do this that just uses a dict like I have in the past (that is not deprecated)?
Solution 1:[1]
Create a list with your dictionaries, if they are needed, and then create a new dataframe with df = pd.DataFrame.from_records(your_list). List's "append" method are very efficient and won't be ever deprecated. Dataframes on the other hand, frequently have to be recreated and all data copied over on appends, due to their design - that is why they deprecated the method
Solution 2:[2]
I also like the append method. But you can do it in one line with a list of dicts
df = pd.concat([df, pd.DataFrame.from_records([{ 'a': 1, 'b': 2 }])])
or using loc and tuples for values on DataFrames with incremenal ascending indexes
df.loc[len(df), ['a','b']] = 1, 2
or maybe
df.loc[len(df), df.columns] = 3, 4
Solution 3:[3]
This have already good answare, but I just wanted to append something at the end here as I didnt find it :) If you are appending a DF to a DF and want to use concat this is the way I found:
original with append
outputxlsx = outputxlsx.append( df, ignore_index=True)
concat
outputxlsx = pd.concat([outputxlsx, pd.DataFrame.from_records(df)])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jsbueno |
| Solution 2 | |
| Solution 3 | beltalowda |
