将列表作为带有索引的新行附加到 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53923012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:13:53  来源:igfitidea点击:

Append list to pandas DataFrame as new row with index

pythonpandasdataframeappendseries

提问by Berlines

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as last row of a dataframe. Imagine I have a simple dataframe:

尽管有许多关于将数据附加到数据帧的堆栈溢出问题,但我无法真正找到以下问题的答案。我正在寻找一个直接的解决方案来附加一个列表作为数据帧的最后一行。想象一下,我有一个简单的数据框:

 indexlist=['one']
 columnList=list('ABC')
 values=np.array([1,2,3])
 # take care, the values array is a 3x1 size array. 
 # row has to be 1x3 so we have to reshape it

values=values.reshape(1,3)
df3=pd.DataFrame(values,index=indexlist,columns=columnList)
print(df3)

     A  B  C
one  1  2  3

After some operations I get the following list:

经过一些操作后,我得到以下列表:

listtwo=[4,5,6]

I want to append it at the end of the dataframe. I change that list into a series:

我想将它附加到数据帧的末尾。我将该列表更改为一个系列:

oseries=pd.Series(listtwo)
print(type(oseries))
oseries.name="two"

now, this does not work:

现在,这不起作用:

df3.append(oseries)

since it gives:

因为它给出:

A   B   C   0   1   2
one 1.0 2.0 3.0 NaN NaN NaN
two NaN NaN NaN 5.0 6.0 7.0

I would like to have the values under A B and C.

我想要 AB 和 C 下的值。

I also tried:

我也试过:

df3.append(oseries, columns=list('ABC'))  *** not working ***
df3.append(oseries, ignore_index=True)  *** working but wrong result
df3.append(oseries, ignore_index=False) *** working but wrong result

df3.loc[oseries.name]=oseries adds a row with NaN values

what I am looking for is a) how can I add a list to a particular index name b) how can I simple add a row of values out of a list even if I don't have a name for index (leave it empty)

我正在寻找的是 a) 如何将列表添加到特定的索引名称 b) 即使我没有索引名称(将其留空),我如何简单地从列表中添加一行值

回答by cs95

Either assign in-place with loc:

要么就地分配loc

df.loc['two'] = [4, 5, 6]
# df.loc['two', :] = [4, 5, 6]
df
     A  B  C
one  1  2  3
two  4  5  6

Or, use df.appendwith the second argument being a Seriesobject having appropriate index and name:

或者,使用df.append第二个参数作为Series具有适当索引和名称的对象:

s = pd.Series(dict(zip(df.columns, [4, 5, 6])).rename('two'))
df2 = df.append(s)

df2
     A  B  C
one  1  2  3
two  4  5  6


If you are appending to a DataFrame without an index (i.e., having a numeric index), you can use locafter finding the max of the index and incrementing by 1:

如果要附加到没有索引的 DataFrame(即具有数字索引),则可以loc在找到索引的最大值并以 1 递增后使用:

df4 = pd.DataFrame(np.array([1,2,3]).reshape(1,3), columns=list('ABC'))
df4

   A  B  C
0  1  2  3

df4.loc[df4.index.max() + 1, :] = [4, 5, 6]
df4
     A    B    C
0  1.0  2.0  3.0
1  4.0  5.0  6.0

Or, using appendwith ignore_index=True:

或者,使用appendwith ignore_index=True

df4.append(pd.Series(dict(zip(df4.columns, [4, 5, 6]))), ignore_index=True)

   A  B  C
0  1  2  3
1  4  5  6