I’d like to iterate row-wise over 2 identically-shaped dataframes, passing the rows from each as vectors to a function without using loops. Essentially something similar to R’s mapply. I’ve investigated a little and the best that I’ve seen uses map in a list comprehension, but I’…
Tag: apply
Mutiple style for one DataFrame issues
I would like to understand how “style” works in python, I always have the same error : AttributeError: ‘Styler’ object has no attribute …. for now, I manage to have : the first row in yellow but not the conditional formatting (df_x0) and the conditional formatting blue and orange…
Pandas apply function to each row by calculating multiple columns
I have been stacked by an easy question, and my question title might be inappropriate. I want to calculate (df.amount * df.con)/df.groupby(‘name’).agg({‘amount’:’sum’}).reset_index().loc(df.name==i).amount) (Sorry, this line will return error, but what I want is to calculat…
Apply a function including if to each row of a dataframe in pandas without for loop
Given a dataframe, I want to get the nonzero values of each row and then find the minimum of absolute values. I want to have a user defined function that does this for me. Also, I do not want to use any for loop since the data is big. My try I get ValueError: The truth value of a Series
Replace value based on a corresponding value but keep value if criteria not met
Given the following dataframe, INPUT df: Cost_centre Pool_costs 90272 A 92705 A 98754 A 91350 A Replace Pool_costs value with ‘B’ given the Cost_centre value but keep the Pool_costs value if the Cost_centre value does not appear in list. OUTPUT df: Cost_centre Pool_costs 90272 B 92705 A 98754 A 91…
Convert a list of strings in pandas into a list of date, and filter the value accordingly
Now I have a dataframe similar as follows What I am trying to do is to Convert all the values in the list to date Filter the value which are only later than 2018-01-01 In the first step, what I tried to do is to use a apply function so that I can cover all elements in the list: However,
Make list after groupby in pandas using apply() function
I have this dataframe: My goal is to keep tracking the values in column2, based on the letters of column1 separated by(:), the output should look like this: What’s the most pythonic way to do this: At the moment I’m able to group by the column 1 and I’m trying to use the apply() function, bu…
Pandas unique values per row, variable number of columns with data
Consider the below dataframe: Assuming my index is unique, I’m looking to retrieve the unique values per index row, to an output like the one below. I wish to keep the empty rows. I have a working, albeit slow, solution, see below. The output number order is not relevant, as long all values are presente…
How to use pandas apply to replace iterrows?
I am calculating the sentiment value on every row in the dataset based on news headline. I used iterrows to achieve this: However, the processing time is taking too long (>30 minutes runtime and it is not done yet). I have 16.6k rows in my dataset. This is a small section of the dataset: I have read that i…
add random elemnt from list to existing dataframe string
I have a dataframe. df[‘Skill’]=python, sql, java. Now for each string I want to add random element (high, low, medium). For Eg: df[‘Skill’]=python:high, sql:low, java:medium. I have tried one code but it adds score[‘low’, ‘high’, ‘medium’] at the en…