[英]how to replace values in 2 dataframe columns based on multiple conditions?
I'm having columns in pandas dataframe which look like this:我在 pandas dataframe 中有列,如下所示:
Positive![]() |
Neutral![]() |
Negative![]() |
---|---|---|
1 ![]() |
0 ![]() |
1 ![]() |
0 ![]() |
1 ![]() |
0 ![]() |
I want it to look like this:我希望它看起来像这样:
Positive![]() |
Neutral![]() |
Negative![]() |
Mixed![]() |
---|---|---|---|
0 ![]() |
0 ![]() |
0 ![]() |
1 ![]() |
0 ![]() |
1 ![]() |
0 ![]() |
0 ![]() |
First, I created column called mixed based on the fact that sentence is both positive and negative.首先,我创建了一个名为混合的列,基于句子既有正面又有负面的事实。 Now, since I already have column "mixed", I do not need double information, so I would like to replace values in positive and negative columnwith 0 (only for mixed sentiment sentences).
现在,由于我已经有“混合”列,我不需要双重信息,所以我想用 0 替换正面和负面列中的值(仅用于混合情感句子)。 I've tried different variation of np.where but nothing seems to understand how to replace value in 2 columns based on condition from these 2 columns.
我尝试了 np.where 的不同变体,但似乎没有人了解如何根据这 2 列的条件替换 2 列中的值。 Any suggestions?
有什么建议么? Thanks:)
谢谢:)
It is a little bit unclear from your question.你的问题有点不清楚。 I guess your question (if i am correct) is to change from this:
我想你的问题(如果我是正确的)是从这个改变:
Positive![]() |
Neutral![]() |
Negative![]() |
---|---|---|
1 ![]() |
0 ![]() |
1 ![]() |
to this (since you have a new column called "mixed"):对此(因为您有一个名为“混合”的新列):
Positive![]() |
Neutral![]() |
Negative![]() |
Mixed![]() |
---|---|---|---|
0 ![]() |
0 ![]() |
0 ![]() |
1 ![]() |
If this is the case, then the code should be (i make the table with 3 rows instead of 1 for easy to see):如果是这种情况,那么代码应该是(为了便于查看,我使用 3 行而不是 1 行制作表格):
import pandas as pd
data = {'Positive': [1, 1, 1], 'Neutral': [0, 0, 0], 'Negative': [1, 1, 1]}
df = pd.DataFrame(data)
print(df)
print(\n)
x = (df['Positive'])
x[1] = 0
print(df)
As the result, in column "Positive", the second row, value is changed from "1" to "0".结果,在第二行的“Positive”列中,值从“1”变为“0”。
With the different index, you can adjust the code by yourself at x[i] = 0
.使用不同的索引,您可以在
x[i] = 0
处自行调整代码。 Similar code applied on "Negative" column.类似的代码应用于“否定”列。
You can just do it in two steps - set the mixed column - then set the pos/neg columns to 0.您可以分两步完成 - 设置混合列 - 然后将 pos/neg 列设置为 0。
>>> df['Mixed'] = 0
>>> df
Positive Neutral Negative Mixed
0 1 0 1 0
1 0 1 0 0
>>> rows = (df.Positive == 1) & (df.Negative == 1)
>>> df.loc[rows, 'Mixed'] = 1
>>> df.loc[rows, ['Positive', 'Negative']] = 0
>>> df
Positive Neutral Negative Mixed
0 0 0 0 1
1 0 1 0 0
You can use df.mask()
if you want to do it all together.如果你想一起做,你可以使用
df.mask()
。
>>> df
Positive Neutral Negative Mixed
0 1 0 1 0
1 0 1 0 0
>>> rows = (df.Positive == 1) & (df.Negative == 1)
>>> df.mask(rows, [0, 0, 0, 1])
Positive Neutral Negative Mixed
0 0 0 0 1
1 0 1 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.