Skip to content

Tag: pandas

Python Pandas – Update column values based on two lists

Suppose I have a table like this Company_Name Product A Apple B Orange C Pear D Lemon Given two lists list1 = [‘Pear’, ‘Lemon’, ‘Apple’, ‘Orange’] list2 = [1, 2, 3, 4] How to replace Product name with the numerical values? The output should look like this &#8211…

Pandas apply condition on a column that contains list

I want to create a new column based on a condition that have to be applied on a list. Here’s a reproducible example: As one can see, each object in the BRAND column is a list that can contain one or more elements (the list can also be empty as for the row where ID = 1). Now, given the

Filter and apply multiple conditions between multiple rows

I have the following dataframe: What I’m trying to do, is to implement the following logic: If there’s only one record (consider the combination of location + method), I’ll just do nothing. That’s the scenario for the first and last row. If there’s more than one record (location …

Using Pandas df.loc

I have a DataFrame of a csv file which is being read by pandas. What I am attempting to do is use df.loc to add a new column but only insert values into the column when values from another column, called “SKU” end with “-RF” and “-NEW”. The code I was working on is below. I…

Substitute numbers in a list of type object pandas

I have a dataframe df looking as follows: What I would like to do is to substitute into df[‘cited_ids’] 0 whenever the corresponding id has d=0 (i) and replace d=1 if there is at least one 0 in the list of df[‘cited_ids’] and the previous d was not 0 (ii). In other words, the first ste…

Pandas take number out string

In my data, I have this column “price_range”. Dummy dataset: I am using pandas. What is the most efficient way to get the upper and lower bound of the price range in seperate columns? Answer Alternatively, you can parse the string accordingly (if you want to limits for each row, rather than the to…