Add a tuple to a specific cell in the pandas frame

Just when I thought I was getting the envy of Python and Pandas, another seemingly simple problem arises. I want to add tuples to specific cells in the pandas frame. These tuples need to be calculated on the fly based on the contents of other cells in the data frame โ€” in other words, I cannot easily calculate all the tuples in advance and add them as one 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 any 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 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 saw data frames with tuples (or lists) in cells - right? Any suggestions on how to get this code would be greatly appreciated.

+5
source share
1 answer

You can use set_value :

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

Also make sure the column is not a floating point column, for example:

 tempDF['newTuple'] = 's' # or set the dtype 

otherwise, you will receive an error message.

+9
source

Source: https://habr.com/ru/post/1211105/


All Articles