Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用np.where在一系列多个值上创建另一个包含1和0的列_Python_Numpy_Pandas_Dataframe_Series - Fatal编程技术网

Python 使用np.where在一系列多个值上创建另一个包含1和0的列

Python 使用np.where在一系列多个值上创建另一个包含1和0的列,python,numpy,pandas,dataframe,series,Python,Numpy,Pandas,Dataframe,Series,嗨,我正在尝试创建一个列。 如果列ChannelID是'J','F','H',那么column name indirect应该是1或者0,我相信在我的csv文件中ChannelID列的每个单元格中都有空格 原始数据 预期数据集 使用的代码 但这段代码在间接列中只给出了0,若我使用下面的代码,那个么我会得到错误- if Output['Channel']== ('J' or 'F' or 'H' or 'G' or 'I'or 'K' or 'M'): Output['Indirect']

嗨,我正在尝试创建一个列。 如果列ChannelID是'J','F','H',那么column name indirect应该是1或者0,我相信在我的csv文件中ChannelID列的每个单元格中都有空格

原始数据 预期数据集 使用的代码 但这段代码在间接列中只给出了0,若我使用下面的代码,那个么我会得到错误-

if Output['Channel']== ('J' or 'F' or 'H' or 'G' or 'I'or 'K' or 'M'):
    Output['Indirect'] = 1
else:
    Output['Indirect'] = 0
error-ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()

由于通道列中每个单元格的值后面都有空格,所以我使用strip删除它,然后得到下面的错误-

if Output['Channel'].strip()== ('J' or 'F' or 'H' or 'G' or 'I'or 'K' or 'M'):
    Output['Indirect'] = 1
else:
    Output['Indirect'] = 0
错误-AttributeError:“Series”对象没有属性“strip”

多谢各位

此外,我还附加了每个单元格中空格的屏幕截图

将列表传递给并使用以下命令转换布尔类型:

以下是从
isin
生成的布尔序列:

In [5]:
df['Channel'].str.strip().isin(['J','F','H'])

Out[5]:
0     True
1     True
2    False
3     True
4     True
5     True
6     True
7     True
8     True
9     True
Name: Channel, dtype: bool

如果需要,您可以使用矢量化的
str.strip
str.upper
对数据进行预处理,以去除字符串中的空格和大写字母。

如果数据中有空格字符,您可以使用
df['Channel'].str.strip().isin(['J','F','H')
没错,另外,你也可以做
.str.upper
来额外保护['Channel'].str.strip().isin(['J','F','H'])给出True和False,而不是1和0@SanchitAluna
.astype(int)
@SanchitAluna你看到我答案上半部分的输出了吗?
if Output['Channel']== ('J' or 'F' or 'H' or 'G' or 'I'or 'K' or 'M'):
    Output['Indirect'] = 1
else:
    Output['Indirect'] = 0
if Output['Channel'].strip()== ('J' or 'F' or 'H' or 'G' or 'I'or 'K' or 'M'):
    Output['Indirect'] = 1
else:
    Output['Indirect'] = 0
In [4]:
df['indirect'] = df['Channel'].str.strip().isin(['J','F','H']).astype(int)
df

Out[4]:
  cd Channel  month  indirect
0  B       J      1         1
1  B       J      3         1
2  B       M      5         0
3  B       J      7         1
4  B       J      9         1
5  B       H      2         1
6  B       J      5         1
7  B       J      6         1
8  B       J      1         1
9  B       J      7         1
In [5]:
df['Channel'].str.strip().isin(['J','F','H'])

Out[5]:
0     True
1     True
2    False
3     True
4     True
5     True
6     True
7     True
8     True
9     True
Name: Channel, dtype: bool