Python大 pandas :逐行填充数据框 [英] Python pandas: fill a dataframe row by row
问题描述
将一行添加到 pandas.DataFrame
对象的简单任务似乎很难完成。有3个stackoverflow问题与此相关,其中没有一个给出工作答案。
The simple task of adding a row to a pandas.DataFrame
object seems to be hard to accomplish. There are 3 stackoverflow questions relating to this, none of which give a working answer.
这是我正在努力做的。我有一个DataFrame,我已经知道它的形状以及行和列的名称。
Here is what I'm trying to do. I have a DataFrame of which I already know the shape as well as the names of the rows and columns.
>>> df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z'])
>>> df
a b c d
x NaN NaN NaN NaN
y NaN NaN NaN NaN
z NaN NaN NaN NaN
现在,我有一个函数迭代地计算行的值。如何使用字典或 pandas.Series
填写其中一行?以下是失败的各种尝试:
Now, I have a function to compute the values of the rows iteratively. How can I fill in one of the rows with either a dictionary or a pandas.Series
? Here are various attempts that have failed:
>>> y = {'a':1, 'b':5, 'c':2, 'd':3}
>>> df['y'] = y
AssertionError: Length of values does not match length of index
显然,它尝试添加一列而不是一行。
Apparently it tried to add a column instead of a row.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3}
>>> df.join(y)
AttributeError: 'builtin_function_or_method' object has no attribute 'is_unique'
非常不知情的错误信息。
Very uninformative error message.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3}
>>> df.set_value(index='y', value=y)
TypeError: set_value() takes exactly 4 arguments (3 given)
显然这只是为了在数据框中设置个别值。
Apparently that is only for setting individual values in the dataframe.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3}
>>> df.append(y)
Exception: Can only append a Series if ignore_index=True
嗯,我不想忽略索引,否则这里是结果:
Well, I don't want to ignore the index, otherwise here is the result:
>>> df.append(y, ignore_index=True)
a b c d
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 1 5 2 3
它将列名与值对齐,但丢失了行标签。
It did align the column names with the values, but lost the row labels.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3}
>>> df.ix['y'] = y
>>> df
a b \
x NaN NaN
y {'a': 1, 'c': 2, 'b': 5, 'd': 3} {'a': 1, 'c': 2, 'b': 5, 'd': 3}
z NaN NaN
c d
x NaN NaN
y {'a': 1, 'c': 2, 'b': 5, 'd': 3} {'a': 1, 'c': 2, 'b': 5, 'd': 3}
z NaN NaN
这也失败了。
那么你怎么样这样做?
推荐答案
df ['y']
将设置列
因为您要设置一行,请使用 .loc
since you want to set a row, use .loc
请注意, .ix
是等效的在这里,您的失败是因为您尝试为行 y
中的每个元素分配字典
可能不是您想要的;转换成一个系列告诉熊猫
你想对齐输入(例如你不需要指定所有的元素)
Note that .ix
is equivalent here, yours failed because you tried to assign a dictionary
to each element of the row y
probably not what you want; converting to a Series tells pandas
that you want to align the input (for example you then don't have to to specify all of the elements)
In [7]: df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z'])
In [8]: df.loc['y'] = pandas.Series({'a':1, 'b':5, 'c':2, 'd':3})
In [9]: df
Out[9]:
a b c d
x NaN NaN NaN NaN
y 1 5 2 3
z NaN NaN NaN NaN
这篇关于Python大 pandas :逐行填充数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!