【问题标题】:How to add new line to existing pandas dataframe? [duplicate]如何向现有的熊猫数据框添加新行? [复制]
【发布时间】:2019-11-17 19:27:42
【问题描述】:

我有一个被定义为空的 pandas 数据框,然后我想在进行一些计算后向其中添加一些行。

我已尝试执行以下操作:

test = pd.DataFrame(columns=['Name', 'Age', 'Gender'])

if #some statement:

test.append(['James', '95', 'M'])

如果我尝试打印然后附加到测试节目

print(test)

test.append(['a', 'a', 'a', 'a', 'a', 'a'])

print(test)

>>>

Empty DataFrame
Columns: [Name, Age, Gender]
Index: []
Empty DataFrame
Columns: [Name, Age, Gender]
Index: []

很明显,这条线没有被添加到数据框中。

我希望输出是

Name | Age | Gender
James | 95 | M

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    append 与字典一起用作:

    test = test.append(dict(zip(test.columns,['James', '95', 'M'])), ignore_index=True)
    
    print(test)
        Name Age Gender
    0  James  95      M
    

    【讨论】:

      【解决方案2】:

      尝试将其附加为字典:

      >>> test = test.append({'Name': "James", "Age": 95, "Gender": "M"}, ignore_index=True)
      >>> print(test)
      

      输出:

          Name Age Gender
      0  James  95      M
      

      【讨论】:

      【解决方案3】:

      你可以通过Series:

      test.append(pd.Series(['James', 95, 'M'], index=test.columns), ignore_index=True)
      

      [出]

          Name Age Gender
      0  James  95      M
      

      【讨论】:

      • 会试一试,你知道这与此处描述的pandas 0.13及以上版本中的loc方法相比效率如何:stackoverflow.com/questions/19365513/…
      • append 通常比其他操作在计算上更昂贵 - 例如concat。从docs 开始,将新行追加到列表中,然后使用concat 可能比迭代地追加许多行更好。
      【解决方案4】:

      追加函数会将列表转换为DataFrame,它会自动生成列[0],但测试有列=['Name','Age','Gender']。并且追加不会改变测试。我说的可能会让人迷惑,运行下面的代码可能会让你明白。

      import pandas as pd
      
      #Append function will convert list into DataFrame,and two dataframe object should have the same column
      data = pd.DataFrame(['James', '95', 'M'])
      print(data)
      
      #right code
      test = pd.DataFrame(columns=['Name', 'Age', 'Gender'])
      test = test.append(pd.DataFrame([['James', '95', 'M']],columns=['Name', 'Age', 'Gender']))
      print(test)
      

      【讨论】:

        【解决方案5】:

        试试这个,

        >>> test.append(dict(zip(test.columns,['James', '95', 'M'])), ignore_index=True)
        
            Name Age Gender
        0  James  95      M
        

        【讨论】:

          【解决方案6】:

          首先append 方法不会就地修改DataFrame,而是返回修改后的(附加版本)。

          第二,传递的新行应该是DataFramedict/Series,或者这些列表。

          #using dict
          row = {'Name': "James", "Age": 95, "Gender": "M"}
          # using Series
          row = pd.Series(['James', 95, 'M'], index=test.columns))
          

          试试print( test.append(row) )看看结果。

          你需要的是将test.append的返回值保存为附加版本,如果你不关心前面的版本,你可以用相同的名字保存,它给了我们:

          test = test.append( row )
          

          【讨论】:

          • 我试过了,它把每一个都添加为一个新行,所以它让 James, 95, M 沿着名字列向下,其余为空白。
          • 是的,我忘了你需要传递 Dataframe 或 dict 或 Series 而不仅仅是一个列表,我修改了答案以使其完整使用其他人的建议。
          猜你喜欢
          • 2019-11-12
          • 1970-01-01
          • 2013-10-22
          • 2017-02-11
          • 1970-01-01
          • 1970-01-01
          • 2021-09-06
          • 2014-08-29
          • 1970-01-01
          相关资源
          最近更新 更多