在 Pandas 中向现有数据帧添加新行时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45104991/
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
Getting error when adding a new row to my existing dataframe in pandas
提问by pyco
I have the below data frame.
我有以下数据框。
df3=pd.DataFrame(columns=["Devices","months"])
I am getting row value from a loop row, print(data)
我从循环行中获取行值,打印(数据)
Devices months
1 Powerbank Feb month
When I am adding this data row to my df3 I am getting an error.
当我将此数据行添加到我的 df3 时,出现错误。
df3.loc[len(df3)]=data
ValueError: cannot set a row with mismatched columns
ValueError:无法设置列不匹配的行
回答by muon
use
用
df3 = pd.concat([df3, data], axis=0)
or as suggested by @Wen use
或者按照@Wen 的建议使用
df3 = df3.append(data)
回答by JoaoMVR
From https://pandas.pydata.org/pandas-docs/stable/merging.html:
从https://pandas.pydata.org/pandas-docs/stable/merging.html:
It is worth noting however, that concat (and therefore append) makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension.
然而,值得注意的是,concat(因此追加)会制作数据的完整副本,并且不断重用此函数会造成显着的性能损失。如果您需要对多个数据集使用该操作,请使用列表推导式。
You should use loc, like you were trying to do, and with a dictionary where the keys are the column names and the values are the data of the row being added.
您应该使用 loc,就像您尝试做的那样,并使用字典,其中键是列名,值是要添加的行的数据。
import pandas as pd
df3 = pd.DataFrame(columns=["Devices","months"])
new_entry = {'Devices': 'device1', 'months': 'month1'}
df3.loc[len(df3)] = new_entry
回答by Akshay
If someone is looking to append new row which is in dictionary format, below will help.
如果有人想添加字典格式的新行,下面会有所帮助。
- Existing DataFrame
- 现有数据帧
In [6]: df Out[6]: Devices months 0 Powerbank Feb month In [7]:
In [6]: df Out[6]: Devices months 0 Powerbank Feb month In [7]:
- Below snippet adds another row to existing DataFrame.
- 下面的代码段向现有 DataFrame 添加了另一行。
In [7]: dictionary_row = {"Devices":"Laptop","months":"Mar month"} In [8]: df = df.append(dictionary_row, ignore_index=True) In [9]: df Out[9]: Devices months 0 Powerbank Feb month 1 Laptop Mar month In [10]:
In [7]: dictionary_row = {"Devices":"Laptop","months":"Mar month"} In [8]: df = df.append(dictionary_row, ignore_index=True) In [9]: df Out[9]: Devices months 0 Powerbank Feb month 1 Laptop Mar month In [10]:
Hope that helps.
希望有帮助。
回答by Santhosh Dhaipule Chandrakanth
As the error suggests the number of columns should of the data being inserted into the dataframe
must match the number of columns of the dataframe
由于错误表明插入的数据dataframe
的列数必须与数据的列数匹配dataframe
>>> df3=pd.DataFrame(columns=["Devices","months"])
>>> df3.loc[len(df3)] = ['Powerbank','Feb']
>>> df3
Devices months
0 Powerbank Feb
>>> data = ['powerbank','feb']
>>> df3.loc[len(df3)] = data
>>> df3
Devices months
0 Powerbank Feb
1 powerbank feb