【问题标题】:Append list to pandas DataFrame as new row with index将列表作为带有索引的新行附加到 pandas DataFrame
【发布时间】:2019-05-24 04:22:49
【问题描述】:

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

 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

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

listtwo=[4,5,6]

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

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

现在,这不起作用:

df3.append(oseries)

因为它给出:

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

我想要 A B 和 C 下的值。

我也试过了:

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

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

【问题讨论】:

    标签: python pandas dataframe append series


    【解决方案1】:

    使用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
    

    或者,使用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
    

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

    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
    

    或者,使用appendignore_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
    

    【讨论】:

    • 谢谢。如果我不使用索引怎么办。只需添加数据行以供以后处理。为什么这不起作用:? df4=pd.DataFrame(np.array([1,2,3]).reshape(1,3),columns=list('ABC')) newlist=[5,6,7] df4.append(newlist)请注意,数据框是故意创建的,没有索引。
    • @Berlines 仅仅因为df4.append 不打算直接使用列表。如果要追加单行,则必须提供一个 pd.Series 对象(您这样做并注意到在对齐列时存在问题),或者使用 loc 并就地分配。
    • @Berlines 确实如此。
    【解决方案2】:

    无索引

    lst1 = [1,2,3]
    lst2 = [4,5,6]
    p1 = pd.DataFrame([lst1])
    p2 = p1.append([lst2], ignore_index = True)
    p2.columns = list('ABC')
    p2
    
        A   B   C
    0   1   2   3
    1   4   5   6
    

    有索引

    lst1 = [1,2,3]
    lst2 = [4,5,6]
    p1 = pd.DataFrame([lst1], index = ['one'], columns = list('ABC'))
    p2 = p1.append(pd.DataFrame([lst2], index = ['two'], columns = list('ABC')))
    p2
    
        A   B   C
    one 1   2   3
    two 4   5   6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-06
      • 2019-06-01
      • 2017-05-21
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      相关资源
      最近更新 更多