• Register
0 votes

Problem :

I am using Pandas 0.20.3 in my python 3.X. I for the first time and want to add one column in a pandas data frame from the other pandas data frame. My both the data frame contains 51 rows. So I tried following code:


But I got following error message:

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

My class_df.dtypes gives me :

Group_ID      object

YEAR           object

Terget           object

phone          object

age              object

Cost            object

RAM           object

Display       object

Storage      object

and when I type(group['phone']) it returns the pandas.core.series.Series

Can anyone suggest me what changes I need to do to remove above error?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I have lots of industry experience in writing complex Python code.

I have gone through your post and understood your problem.

So to begin with you must follow below approach to resolve your issue:

I did lot of research and ultimately found that this error comes when you return an empty data frame. The best approach which ultimately worked for me was to check if the data frame is empty just before using apply() as follows:

if len(df) !=  0:
    df['indicator'] = df.apply(assign_indicator, axis=1)
9 7 4
38,600 points

Related questions

0 votes
1 answer 10 views
Problem: I have a dynamic DataFrame that works fine, but when there is no data to add to the DataFrame, I get an error. And so I need a solution to create an empty DataFrame with only column names. Until then, I have something like this: df = pd.DataFrame(columns=COLUMN_NAMES) # Note ... "dataframe"> <tbody> <tr> <td>Index([], dtype='object')</td> <td>Empty DataFrame</td> </tr> </tbody> </table>
asked Dec 23, 2020 sasha 6.4k points
0 votes
1 answer 683 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24, 2020 jwilliam 3.9k points
1 vote
1 answer 469 views
Problem : I am facing bellow strange error undefinedmetricwarning: precision is ill-defined and being set to 0.0 due to no predicted samples.
asked Nov 8, 2019 peterlaw 6.9k points
2 votes
2 answers 623 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6, 2020 samhaz 5k points
0 votes
1 answer 274 views
Problem: I am trying deletesome column and convert some value to column with df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True) df2['date'] = df2['date'].map(lambda x: str(x)[1:]) df2['date'] = df2['date'].str.replace(':', ' ', 1) ... : A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead What's the problem?
asked Nov 14, 2020 sasha 6.4k points
1 vote
1 answer 329 views
Problem : I am very new to Python. While trying to execute my code I am facing below warning C:\Python27\lib\site-packages\pandas\core\indexing.py:411: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer ... '] = mydf.review_text.map(lambda x: tokenizer.tokenize(x)) print mydf[:3] I am looking for expert help to fix above error.
asked Apr 27, 2020 stewart 4k points
0 votes
1 answer 12 views
Problem: So I know that I should avoid using globals in the first place because of confusion like this, but if I were to use them, could I use them correctly? (I am trying to call a global copy of a variable created in a separate function.) x = somevalue def func_A ... me i am in a search of answer actually i donot have idea that how to define vaiable globally and how i can edit its value anyone ?
asked Nov 10, 2020 Han Li 710 points
1 vote
1 answer 155 views
Problem: I am new in python programming language and I am very confused about programming logics. Supose, someone has given two already defined variables, i and j, write a statement that swaps their associated values. Can anyone help me with this problem?
asked Apr 9, 2020 LizzyM 6.1k points