Append()方法的一个很好的替代方法,现在它已经被弃用了?

我经常使用下面的方法将一行附加到数据框架。我真正喜欢它的一点是,它允许您附加一个简单的 dict 对象。例如:

# Creating an empty dataframe
df = pd.DataFrame(columns=['a', 'b'])


# Appending a row
df = df.append({ 'a': 1, 'b': 2 }, ignore_index=True)

同样,我最喜欢的是这段代码非常干净,只需要很少的几行代码。现在我想建议的替代方案是:

# Create the new row as its own dataframe
df_new_row = pd.DataFrame({ 'a': [1], 'b': [2] })
df = pd.concat([df, df_new_row])

所以之前的一行代码现在是两行带有一个一次性变量和额外的代码,我在这里创建了一个新的数据框架。: (有没有一个好的方法来做到这一点,只是使用一个字典,就像我在过去(这是不推荐的) ?

79700 次浏览

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

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

If you want to use concat instead:

append

outputxlsx = outputxlsx.append(df, ignore_index=True)

concat

outputxlsx = pd.concat([outputxlsx, df])

i also have the problem when using DataFrame.append in my program before, but it has been fixed now. Hopefully this snippet can help so.

import pandas as pd
df1=pd.DataFrame(dict_1)


def addData(param1,param2,param3):
dict_2={"list1":var1, "list2":var2, "list3":var3}
df2=pdDataFrame(dict_2, index={len(dict_2)+1})
dfc=pd.concat([df1, df2])
return dfc

I was facing a similar issue. The other solutions weren't really working for me. I'm leaving this answer here as an additional possibility to deal with the issue since this is the first google result for certain searches and I myself ended here at least for the second time.

In my case the data is not a dict but just a list of values for a known set of parameters. I want to add the parameter values to a dataframe as rows because this way I can access a series of all the values for one parameter via df[parameter].

I start with an empty DataFrame:

parameters = ['a', 'b', 'c', 'd', 'e', 'f']
df = pd.DataFrame(columns=parameters)

df:

        a   b   c   d   e   f

With append I could add rows very convenient like so:

new_row = pd.Series([1,2,3,4,5,6], index=parameters, name='row1')
df.append(new_row)

df:

        a   b   c   d   e   f
row1    1   2   3   4   5   6

With pd.concat I found this to deliver the same result in very similar way:

new_row = pd.DataFrame([1,2,3,4,5,6], columns=['row1'], index=parameters).T
df = pd.concat((df, new_row))

The key was to create a (1,n) dataframe from the 1d data and then transpose it to match the other dataframe.

# Deprecated issue has been resolved


# Creating an empty dataframe
df = pd.DataFrame(columns=['a', 'b'])
print("df columns:", df)


# Appending a row
df = df.append({ 'a': 1, 'b': 2 }, ignore_index=True)
print("df column Values :", df)


# Create the new row as its own dataframe
df_new_row = pd.DataFrame.from_records({ 'a': [3], 'b': [4] })
df = pd.concat([df, df_new_row])
print("pd concat with two df's :", df)

For those, like me, who want a descriptive function rather than lots of one-liners, here is an option based on @Rafael Gaitan above.

def appendDictToDF(df,dictToAppend):
df = pd.concat([df, pd.DataFrame.from_records([dictToAppend])])
return df


# Creating an empty dataframe
df = pd.DataFrame(columns=['a', 'b'])


# Appending a row
df= appendDictToDF(df,{ 'a': 1, 'b': 2 })