0

Example i'm new to python and stuck with this problem so i want to compare between column rosyi and azmi, if the column have "_" the result will be "other" and if the column have "http" with the same value the result will be "agreed" would anyone suggest what library and how to use it

i've tried compare with

import pandas as pd
import numpy as np
df = pd.read_excel('rosyi.xlsx')
df['Diff'] = np.where(df['Rosyi'] & df['Azmi'] , '0', '1')
df

but the result with _ and https will be the same

Result

i understood that this isnt a method that i want, but im stuck very hard

Mark
  • 7,785
  • 2
  • 14
  • 34
  • 3
    Hi Muhammad! Welcome to StackOverflow. ***Do not include pictures of your data. Include your data by running df.to_dict() or df.head().to_dict().*** See: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Mark Aug 19 '23 at 14:59

1 Answers1

0

It is not fully clear what you want to happen if one column meets the _ or http condition but have different values. The code below, based on a simple dateset, shows the general approach which can be used. np.where tests a condition and gives different values if True or False

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': ['_','_','_','http://text','_','_','_','_','_'],
                   'b': ['_','_','_','http://text','_','_','_','_','x']
                   })

df['diff'] = np.where((df['a'] == '_') & (df['b'] == '_'), 'other',
                    np.where((df['a'].str.contains('http')) & (df['a'] == df['b']),
                    'agreed', 'error'))

print(df)

gives:

             a            b    diff
0            _            _   other
1            _            _   other
2            _            _   other
3  http://text  http://text  agreed
4            _            _   other
5            _            _   other
6            _            _   other
7            _            _   other
8            _            x   error
user19077881
  • 3,643
  • 2
  • 3
  • 14