Excel Python 比较

问题描述 投票:0回答:1

Example 我是 python 新手并且遇到了这个问题 所以我想在 rosyi 和 azmi 列之间进行比较,如果列有“_”,结果将是“其他”,如果列有“http”且具有相同的值,结果将是“同意” 有人会建议什么库以及如何使用它

我尝试过比较

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

但是_和https的结果是一样的

结果

我知道这不是我想要的方法,但我很难坚持下去

python pandas excel named-entity-recognition
1个回答
0
投票

尚不完全清楚如果一列满足 _ 或 http 条件但具有不同的值,您想要发生什么。下面的代码基于一个简单的日期集,显示了可以使用的一般方法。

np.where
测试条件并在 True 或 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)

给出:

             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
© www.soinside.com 2019 - 2024. All rights reserved.