本文介绍了将一个元组添加到大 pandas 数据帧的特定单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是当我以为我正在摆脱Python和熊猫时,另一个看似简单的问题就出现了。我想添加元组到大熊猫数据帧的特定单元格。这些元组需要根据数据帧中其他单元格的内容即时计算 - 换句话说,我不能提前计算所有元组,并将它们作为单个数组添加。



作为一个例子,我用一些数据定义了一个数据框,并添加了几个空列:

  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

我可以滚动newValue列的每个单元格,并添加一个整数值,没有问题:

  anyOldValue = 3.5 
for i in range(10):
tempDF.ix [(i,'newValue')] = anyOldValue

print tempDF

但是,如果我尝试添加一个元组,我会收到一条错误消息:

  anyOldTuple =(2.3,4.5)
for i in range(10):
temp DF.ix [(i,'newTuple')] = anyOldTuple

打印tempDF

我收到了几个错误信息,包括:

  ValueError:设置ndarray时必须具有相同的len键和值

...和...

  ValueError:设置一个有序列的数组元素。 

我确定我已经看到了单元格中的元组(或列表)的数据框 - 避风港我不是吗任何建议,如何使这个代码工作将非常感谢。

解决方案

您可以使用 set_value

  tempDF.set_value(i,'newTuple',anyOldTuple)

还要确保列不是浮动列,例如:

  tempDF ['newTuple'] ='s'或设置dtype 

否则您将收到错误。


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.

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:

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.

解决方案

You can use 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.

这篇关于将一个元组添加到大 pandas 数据帧的特定单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 15:07