需要在pandas/python中使用3个不同的值列表验证3个不同的pandas列。问题是它覆盖了第一次确认的状态。
import pandas as pd
data = pd.DataFrame({'freq': ['Dly', 'ad','Weekly ', 'XXX'],
'typ': ['TEST', 'A','TEST2', 'YYYY'],
'category': ['ACC', 'T','TEST3', 'ZZZZ'],
'id': ['1', '2','3', '4']})
3 lists:
freq_list = ['ad','annex','Qtr','Weekly']
type_list= ['A','B']
catry_list = ['T','C','ACC']
Code:
df['status'] = df.apply(lambda x: x.typ in type_list, axis=1)
df['status'] = df.apply(lambda x: x.freq in freq_list, axis=1)
df['status'] = df.apply(lambda x: x.category in catry_list, axis=1)
Not able to detect this as it is overiding one status with other status w.r.t column.
Expected output:
freq typ category id status
Dly TEST ACC 1 freq and type does not exist in list
ad A T 2
Weekly TEST2 TEST3 3 typ and category does not exist
XXX YYYY ZZZZ 4 Freq, typ and cateogry does not exist
2条答案
按热度按时间axr492tv1#
你可以试试这样的方法:
输出:
inn6fuwd2#
你可以定义一个函数如下: