Change one column with one of multiple strings from another column if condition is met

pandas replace values in column based on multiple condition
if statement across multiple columns
pandas create new column based on multiple condition
excel if statement multiple conditions range
lookup and return multiple values concatenated into one cell
vlookup return multiple values in one cell separated by comma
if function excel
if formula excel multiple conditions

I want to populate one column with a string (one of many) contained in another column (if it is contained in that column)

Right now I can do it by repeating the line of code for every different string, I'm looking for the more efficient way of doing it. I have about a dozen in total.

df.loc[df['column1'].str.contains('g/mL'),'units'] = 'g/mL'
df.loc[df['column1'].str.contains('mPa.s'),'units'] = 'mPa.s'
df.loc[df['column1'].str.contains('mN/m'),'units'] = 'mN/m'

I don't know how to make it to check

df.loc[df['column1'].str.contains('g/mL|mPa.s|mN/m'),'units'] = ...

And then make it equal to the one that is contained.

Use str.extract:

# example dataframe
df = pd.DataFrame({'column1':['this is test g/mL', 'this is test2 mPa.s', 'this is test3 mN/m']})

0    this is test g/mL
1  this is test2 mPa.s
2   this is test3 mN/m
df['units'] = df['column1'].str.extract('(g/mL|mPa.s|mN/m)')

               column1  units
0    this is test g/mL   g/mL
1  this is test2 mPa.s  mPa.s
2   this is test3 mN/m   mN/m

Excel IF statement with multiple AND/OR conditions, nested IF , The formula tells Excel to return "Pass" if a value in column C >=20 AND a value in column D >=30. Excel returns TRUE if at least one of the specified conditions is met. errors in Excel formulas and replace them with another calculation, If you mean A1 contains exactly that text string, use this formula:. To use a formula to sum values in Column B based on Column A, you can create a formula based on the SUMIF function.Just do the following steps: #1 select the text values in Column A (A1:A6), press Ctrl +C to copy these values, and paste into another blank column (Column D).

Use loop with str.contains:

L = ['g/mL', 'mPa.s', 'mN/m']
for val in L:
    df.loc[df['column1'].str.contains(val),'units'] = val

Or Series.str.extract with list of all possible values:

L = ['g/mL', 'mPa.s', 'mN/m']
df['units'] = df['column1'].str.extract('(' + '|'.join(L) + ')')

Lookup and return multiple values concatenated into one cell, Unique strings [UDF] However if your Excel version is missing the TEXTJOIN function you can Looks for a value in a column and returns a value on the same row from a to return multiple values using vlookup in excel with a minor change. This is my vlookup function in another cell and it does work. This can be copied down to return the data from column A of the first sheet where there is a number in the corresponding cell of column H. The term ROWS($1:1) will return the value 1 on the first row of the formula, but when the formula is copied down it changes to ROWS($1:2), ROWS($1:3), ROWS($1:4), and so on, which return the values 2, 3, 4

Actually, according to the docs you can exactly do that using the regex=True parameter!

df.loc[df['column1'].str.contains('g/mL|mPa.s|mN/m', regex=True),'units'] = ...

5 ways to apply an IF condition in pandas DataFrame, If so, in this tutorial, I'll show you 5 different ways to apply such a condition. Set of numbers and lambda; Strings; Strings and lambada; OR condition Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). df['​column name'].apply(lambda x: 'value if condition is met' if x condition else  As bellow screenshot shown, you want to sum numbers in column B until reaching a certain value D in adjacent column of column A. Here you will sum number 2+2+3+4 in column B in Excel. This tutorial will introduce method for you to achieve it step by step. Sum a column until a condition is met in adjacent column with formula

Linux Recipes for Oracle DBAs, $usedSpc" esac If BOX=$(uname -a | awk '{print $2}') echo $arcStat | mail -s free) command is piped to the awk command, which extracts the fifth column. In those situations, you can modify the script to use egrep to filter out multiple strings​. When a condition is met, the case statement runs any statements within the  What if I wanted to remove text from a string in one column based on the text in another column using functions, when there is no other clear delineation such as a space? Using JChris's original data as an example: What if I wanted to remove the CarName text from the Year column based on it appearing in the CarName column?

Master VLOOKUP Multiple Criteria and Advanced Formulas , This value starts at 1 and goes up relative to the number of columns in your table. Concatenation is a great way to account for multiple-string search criteria. INDEX-MATCH is a combination formula that can replace your VLOOKUP that the condition you set and returns one value if it is met, and another if it is not met. In this article, we will first give an overview on SSIS derived column transformation, then we will run an experiment to check if there is any difference between adding multiple expressions within one derived column transformation and adding a derived column transformation for each expression.

How to Update a Column Based on a Filter of Another , So to perform an update based on the value of another column in the same you also have the possibility to do updates where multiple columns meet the criteria if you want to update rows in one table based on the condition of another table​? used around strings, Backticks are used around table and column identifiers. Another thing to note is that I could also do a range between two values which is essentially multiple conditions for a conditional column. What I had to first do was to change the Amount column from Text to Whole Number, so that my conditions would work.

  • How do you make it = the character it matched?