Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

Tags:

python

I'm using Pandas 0.20.3 in my python 3.X. I want to add one column in a pandas data frame from another pandas data frame. Both the data frame contains 51 rows. So I used following code:

class_df['phone']=group['phone'].values 

I got following error message:

ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series 

class_df.dtypes gives me:

Group_ID         object YEAR             object Terget           object phone            object age              object 

and type(group['phone']) returns pandas.core.series.Series

Can you suggest me what changes I need to do to remove this error?

The first 5 rows of group['phone'] are given below:

0    [735015372, 72151508105, 7217511580, 721150431... 1                                                   [] 2    [735152771, 7351515043, 7115380870, 7115427... 3    [7111332015, 73140214, 737443075, 7110815115... 4    [718218718, 718221342, 73551401, 71811507... Name: phoen, dtype: object 
like image 901
Tanvi Mirza Avatar asked Jan 17 '18 17:01

Tanvi Mirza


Video Answer


2 Answers

In most cases, this error comes when you return an empty dataframe. The best approach that worked for me was to check if the dataframe is empty first before using apply()

if len(df) !=  0:     df['indicator'] = df.apply(assign_indicator, axis=1) 
like image 169
Jefferson Sankara Avatar answered Sep 19 '22 14:09

Jefferson Sankara


You have a column of ragged lists. Your only option is to assign a list of lists, and not an array of lists (which is what .value gives).

class_df['phone'] = group['phone'].tolist() 
like image 36
cs95 Avatar answered Sep 16 '22 14:09

cs95