Python panda overwrite value based on conditional in other column
NickName:jeangelj Ask DateTime:2018-04-04T02:01:23

Python panda overwrite value based on conditional in other column

Working with the dataframe df in python pandas:

Product_ID  | Category  | Sub_Cat
32432           0         Gadget
24085         Big Tech    Computer
54398         Small Tech  Gadget
97456           0         Computer

I am working on a new column, where I will over-write the Sub_Cat value with the Category value, if it is not 0.

This is the output I am looking for:

Product_ID  | Category  | Sub_Cat         | Cat_for_Analysis
32432           0         Gadget            Gadget
24085         Big Tech    Computer          Big Tech
54398         Small Tech  Gadget            Small Tech
97456           0         Computer          Computer

Thank You!

Copyright Notice:Content Author:「jeangelj」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49636459/python-panda-overwrite-value-based-on-conditional-in-other-column

Answers
root 2018-04-03T18:47:18

Using np.where:\n\ndf['Cat_for_Analysis'] = np.where(df['Category'] == '0', df['Sub_Cat'], df['Category'])\n\n\nOr equivalently the negated version, if it makes more intuitive sense based on your problem:\n\ndf['Cat_for_Analysis'] = np.where(df['Category'] != '0', df['Category'], df['Sub_Cat'])\n\n\nThe resulting output for either method:\n\n Product_ID Category Sub_Cat Cat_for_Analysis\n0 32432 0 Gadget Gadget\n1 24085 Big Tech Computer Big Tech\n2 54398 Small Tech Gadget Small Tech\n3 97456 0 Computer Computer\n",


BENY 2018-04-03T18:04:35

You can using ffill after replace '0' to np.nan\n\ndf['Cat_for_Analysis']=df.replace('0',np.nan)[['Category','Sub_Cat']].bfill(1).iloc[:,0]\ndf\nOut[876]: \n Product_ID Category Sub_Cat Cat_for_Analysis\n0 32432 0 Gadget Gadget\n1 24085 BigTech Computer BigTech\n2 54398 SmallTech Gadget SmallTech\n3 97456 0 Computer Computer\n",


More about “Python panda overwrite value based on conditional in other column” related questions

Python panda overwrite value based on conditional in other column

Working with the dataframe df in python pandas: Product_ID | Category | Sub_Cat 32432 0 Gadget 24085 Big Tech Computer 54398 Small Tech Gadget 97456 ...

Show Detail

How to Overwrite a Series Value through Conditional Expression?

I'm trying to overwrite a new column within a DataFrame based on a conditional expression: if df['service'] == 'PE1' or 'PE2': Change the existing value in df['service'] to equal the original df[&

Show Detail

Conditional formatting based on other column

I have a table, that consists of 2 columns. I need to highlight values in first column, that are more or equal than 10% of the number in the second column. I created additional row there, to clari...

Show Detail

Conditional Formatting on column based on other column value

I am trying to build a simple conditional formatting that will allow me color a cell value if its value is below another cell's value in the same row. For example - As we can see, In Rows 2 and 5 ...

Show Detail

Conditional formatting based on cell in different column but same row

I apologize if this has been posted, but I haven't found a solution that works. I have an excel sheet with a lot of data. I want to make the cells in a certain column (column CG) turn purple if...

Show Detail

Highlight Cell in one Column BASED on Conditional Formatting of Other Columns

I am a beginner with Macros. I slightly changed my code below to include conditional formatting based on an Input Value. The code below applies a conditional format (orange highlight) to 6 specific

Show Detail

Conditional formatting for a column based on the value of header

I have several columns where the data from rows 7 onwards changes depending on the selection made from a dropdown menu in row 6. Most of these options will result in textual or number based values

Show Detail

New column in panda, Reincremental based on value

I want to create a new column in panda where the order is incremental if title column is the same as previous row. If title is different, restart the count. How can I do this? original_df title mu...

Show Detail

Finding matches in Panda DataFrame by matching a Panda DataFrame

Trying to return the rows of an array where a column matches the values of another array. I start with elm value and call the nearby function to find other elms near elm based on searching the df ...

Show Detail

How to add value to column conditional on other column

In pandas, how is it possible to add a value to a column conditional on the string in other column? (something resembling a 2-dimensional slice operation?) For example, having a dataframe like thi...

Show Detail