'replace values by different conditions in a dataframe
I have a dataframe like this:
df_test = pd.DataFrame({'ID1':['A','B','C','BA','BA','AB','>','>','>','>'],
'ID2':['','','','','','','mh','mh','nn','nn']})
df_test
I want to obtain a dataframe like this based on the column 'ID1'(1. if len(ID1)>2: then ID1=ID1[-1](for example 'BA', 'AB' will be replaced with 'A', 'B', respectively); 2. if ID1='>': then ID1=ID2(for example: '>' will be replaced with 'mh','nn',respectively)):
df_result = pd.DataFrame({'ID1':['A','B','C','A','A','B','mh','mh','nn','nn']})
df_result
Solution 1:[1]
You can use .str[-1] regardless of the length of the strings in the column to select the last character, and use <column>.where(cond, other_col) to fill in values that don't match cond with those values from other_col:
df_test['ID1'] = df_test.assign(ID1=df_test['ID1'].str[-1]).pipe(lambda x: x['ID1'].where(x['ID1'] != '>', x['ID2']))
Solution 2:[2]
Let us do mask
df_test.ID1.mask(df_test.ID1.eq('>'),df_test.ID2,inplace=True)
df_test
Out[217]:
ID1 ID2
0 A
1 B
2 C
3 BA
4 BA
5 AB
6 mh mh
7 mh mh
8 nn nn
9 nn nn
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | BENY |


