Dec-14-2022, 01:32 PM
Can do it like this.
so if use
import pandas as pd import numpy as np df = pd.DataFrame({ 'col1': [1, 2, 3, 4, 10], 'col2': [1, 4, 6, 8, 10] }) df['result'] = np.where(df['col1'] == df['col2'], '*' + df['col2'].astype(str), df['col1']) print(df)
Output: col1 col2 result
0 1 1 *1
1 2 4 2
2 3 6 3
3 4 8 4
4 10 10 *10
(Dec-14-2022, 07:32 AM)paul18fr Wrote: so I do not see any difficulty using at least "if ... then ... else".Pandas is very different beast🦄 and not like ordinary Python code,
so if use
if/else
or a for loop
then are in most cases doing something wrong or very ineffective in Pandas.