简体   繁体   English

如何根据多个条件替换 2 个 dataframe 列中的值?

[英]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 1 0 0 1 1
0 0 1 1 0 0

I want it to look like this:我希望它看起来像这样:

Positive积极的 Neutral中性的 Negative消极的 Mixed混合
0 0 0 0 0 0 1 1
0 0 1 1 0 0 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 1 0 0 1 1

to this (since you have a new column called "mixed"):对此(因为您有一个名为“混合”的新列):

Positive积极的 Neutral中性的 Negative消极的 Mixed混合
0 0 0 0 0 0 1 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.

相关问题 如何根据多个条件替换Dataframe中的值? - How to replace values in Dataframe based on multiple conditions? 如何根据 pandas 中多列的条件替换列中的值 - How to replace values in a column based on conditions from multiple columns in pandas 如何用python DataFrame中的条件替换列的值? - How to replace the values of columns with conditions in python DataFrame? 如何根据条件替换熊猫数据框中的值? - How to replace values in a pandas dataframe based on conditions? 在某些条件下,如何根据不同的数据框列替换一个数据框的列值? - How can we replace the columns values of one dataframe based on different dataframe column using some conditions? 根据不同长度的多个条件替换数据帧值 - Replace dataframe values based on multiple conditions of different length's 根据 Panda 数据框中的条件为多个列分配不同的值 - Assign multiple columns different values based on conditions in Panda dataframe 如何基于两个条件替换数据框中的所有值 - How to replace all values in a dataframe based on two conditions 如何根据多个条件在 Pandas 数据框中插入列? - How to insert columns in a pandas dataframe based on multiple conditions? 如何基于多个列和条件填充pandas DataFrame? - How to populate pandas DataFrame based on multiple columns and conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM