【发布时间】:2021-12-25 10:15:50
【问题描述】:
我一直在尝试根据另一个包含字符串的列值来更新数据框中的列值。
import pandas as pd
import numpy as np
1. df=pd.read_excel('C:\\Users\\bahlrajesh23\\datascience\\Invoice.xlsx')
2. df1 =( df[df['Vendor'].str.contains('holding')] )
3. df['cat'] = pd.np.where(df['Vendor'].str.contains('holding'),"Yes",'' )
4. print(df[0:5])
上面第 4 行的代码运行良好,但现在我想在第 3 行添加更多条件,我像这样修改了上面的第 3 行。
df['cat'] = pd.np.where((df['Vendor'].str.contains('holding'),"Yes",''),
(df['Vendor'].str.contains('tech'),"tech",''))
我收到以下错误
ValueError: either both or neither of x and y should be given
我怎样才能做到这一点?
【问题讨论】:
标签: python pandas dataframe search contains