将元组添加到 Pandas 数据帧的特定单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27949671/
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
Add a tuple to a specific cell of a pandas dataframe
提问by user1718097
Just when I thought I was getting the hang of Python and Pandas, another seemingly simple issue crops up. I want to add tuples to specific cells of a pandas dataframe. These tuples need to be calculated on-the-fly based on the contents of other cells in the dataframe - in other words, I can't easily calculate all tuples in advance and add them as a single array.
就在我以为我已经掌握了 Python 和 Pandas 的窍门时,另一个看似简单的问题突然出现了。我想将元组添加到 Pandas 数据帧的特定单元格。这些元组需要根据数据帧中其他单元格的内容即时计算 - 换句话说,我无法轻松提前计算所有元组并将它们添加为单个数组。
As an example, I define a dataframe with some data and add a couple of empty columns:
例如,我定义了一个包含一些数据的数据框并添加了几个空列:
import pandas as pd
import bumpy as np
tempDF = pd.DataFrame({'miscdata': [1.2,3.2,4.1,2.3,3.3,2.5,4.3,2.5,2.2,4.2]})
tempDF['newValue'] = np.nan
tempDF['newTuple'] = np.nan
I can scroll through each cell of the 'newValue' column and add an integer value without problems:
我可以滚动浏览“newValue”列的每个单元格并添加一个整数值而不会出现问题:
anyOldValue = 3.5
for i in range(10):
tempDF.ix[(i,'newValue')] = anyOldValue
print tempDF
However, if I try to add a tuple I get an error message:
但是,如果我尝试添加元组,则会收到一条错误消息:
anyOldTuple = (2.3,4.5)
for i in range(10):
tempDF.ix[(i,'newTuple')] = anyOldTuple
print tempDF
I've received several error messages including:
我收到了几条错误消息,包括:
ValueError: Must have equal len keys and value when setting with an ndarray
…and…
…和…
ValueError: setting an array element with a sequence.
I'm sure I've seen data frames with tuples (or lists) in the cells - haven't I? Any suggestions how to get this code working would be much appreciated.
我确定我在单元格中看到过带有元组(或列表)的数据框 - 不是吗?任何有关如何使此代码工作的建议将不胜感激。
回答by elyase
You can use set_value:
您可以使用set_value:
tempDF.set_value(i,'newTuple', anyOldTuple)
Also make sure that the column is not a float column, for example:
还要确保该列不是浮动列,例如:
tempDF['newTuple'] = 's' # or set the dtype
otherwise you will get an error.
否则你会得到一个错误。
回答by J.Melody
set_value is deprecated.
set_value 已弃用。
you can just use .at[] or iat[]
你可以只使用 .at[] 或 iat[]
e.g. some_df.at[ idx, col_name] = any_tuple
例如 some_df.at[ idx, col_name] = any_tuple
回答by Markus Dutschke
As J.Melodypointed out, .at[]and .iat[]can be used to assign a tuple to a cell, if the dtype of the column is object.
正如J.Melody指出的,.at[]并且.iat[]可用于一元组分配给一个小区,如果列的D型是object。
Minimal example:
最小的例子:
df initialized as:
a b c
0 0 1 2
1 3 4 5
2 6 7 8
df containing tuple:
a b c
0 0 (1, 2) 2
1 3 4 5
2 6 7 8
Code:
代码:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.arange(9).reshape((3,3)), columns=list('abc'), dtype=object)
print('df initialized as:', df, sep='\n')
df.at[0,'b'] = (1,2)
print()
print('df containing tuple:', df, sep='\n')
Note:
笔记:
If you skip , dtype=object, you end up with
如果你跳过, dtype=object,你最终会得到
ValueError: setting an array element with a sequence.

