apply

truth value of a Series is ambiguous

truth value of a Series is ambiguous Question: I am trying to create new column in pandas dataframe with row number as condition using iloc. This is my code: import pandas as pd shop=pd.DataFrame.from_dict(data) def cate_shop (shop): if shop==shop.iloc[:4]: return ‘Service’ if shop==shop.iloc[4:140]: return ‘Food & Beverage’ if shop==shop.iloc[140:173]: return ‘Fashion’ if shop==shop.iloc[173:197]: return ‘Electronics’ …

Total answers: 1

KeyError(key) in get_loc_level after using .transform() or apply()

KeyError(key) in get_loc_level after using .transform() or apply() Question: I have a large grouped data frame with multiple groups where I’m trying to filter rows within each group. To simplify it, I will share a simplified data frame with one group where I’m getting the error. df5 is grouped by "Detail", "ID", "Year" data2 = …

Total answers: 1

How to get values outside an interval pandas DataFrame

How to get values outside an interval pandas DataFrame Question: I’m trying to get the values outside an interval in pandas dataframe, and I was trying to avoid iterating over the rows. is there any way to do that? This is what I was trying, but it gives the error ValueError: The truth value of …

Total answers: 3

Impute column based on another column is NaN or not

Impute column based on another column is NaN or not Question: I have a simple df here. So basically for Null values in ["A"] if the same row has no null value for ["B"] then make the ["A"] equal to ["B"] and if they are both null then it should just skip. Code: test = …

Total answers: 1

Create new column using custom function pandas df error

Create new column using custom function pandas df error Question: I want to create a new column which gives every row a category based on their value in one specific column. Here is the function: def assign_category(df): if df[‘AvgVAA’] >= -4: return ‘Elite’ elif df[‘AvgVAA’] <= -4 and df[‘AvgVAA’] > -4.5: return ‘Above Average’ elif …

Total answers: 1

Optimizing apply and lambda function with pandas

Optimizing apply and lambda function with pandas Question: I am trying to optimize a function returning the value (wage)of a variable given a condition (largest enrollment within MSA) for every year. I thought combining apply and lambda would be efficient, but my actual dataset is large (shape of 321681×272) making the computation extremely slow. Is …

Total answers: 1

Apply a function row by row using other dataframes' rows as list inputs in python

Apply a function row by row using other dataframes' rows as list inputs in python Question: I’m trying to apply a function row-by-row which takes 5 inputs, 3 of which are lists. I want these lists to come from each row of 3 correspondings dataframes. I’ve tried using ‘apply’ and ‘lambda’ as follows: sol[‘tf_dd’]=sol.apply(lambda tsol, …

Total answers: 3