pandas 如何更改 Dataframe 中列的特定值?

8xiog9wr  于 2023-04-18  发布在  其他
关注(0)|答案(6)|浏览(160)

我有以下dataframe:
| 情感_标签|情绪_分数|
| --------------|--------------|
| 满意了|0.882621|
| 失望|0.826413|
| 满意了|0.893627|
| 满意了|0.863469|
| 失望|0.982447|
我想将所有“失望”值的情绪得分减去1。这将是所需的输出:
| 情感_标签|情绪_分数|
| --------------|--------------|
| 满意了|0.882621|
| 失望|0.173587|
| 满意了|0.893627|
| 满意了|0.863469|
| 失望|0.017553|
我尝试使用groupby()方法将值拆分为两个不同的列,但生成的NaN值使执行额外计算变得困难。我还希望保持列相同。

数据框架

df = pd.DataFrame({'Sentiment_Label': ["Satisfied", "Disappointed", "Satisfied", "Satisfied", "Disappointed"],
                   'Sentiment_Score': [0.882621, 0.826413, 0.893627, 0.863469, 0.982447],
                  })
af7jpaap

af7jpaap1#

你也可以使用np.where函数,它的工作原理与MS-Excel的if函数完全相似。

import numpy as np
( df['Sentiment_Score']= 
np.where(df.Sentiment_Label='Disappointed',df['Sentiment_Score']-1,
df['Sentiment_Score']
)
c90pui9n

c90pui9n2#

您可以尝试以下操作:

df.loc[df['Sentiment_Label'] == 'Disappointed', 'Sentiment_Score'] -= 1

通过应用此方法,您将获得:
| 情感_标签|情绪_分数|
| --------------|--------------|
| 满意了|0.882621|
| 失望| -0.173587 |
| 满意了|0.893627|
| 满意了|0.863469|
| 失望| -0.017553 |

d4so4syb

d4so4syb3#

你可以在每一行上使用apply(),然后定义一个函数,如果Sentiment_Label的值是Disappointed,则该函数执行1 - Sentiment_Score

# Function to apply to each row of dataframe
def sub_1(x):
    if x['Sentiment_Label'] == 'Disappointed':
        x["Sentiment_Score"] = 1-x["Sentiment_Score"]
    return x

# Dataframe definition
df = pd.DataFrame({
    'Sentiment_Label': ["Satisfied", "Disappointed", "Satisfied", "Satisfied", "Disappointed"],
    'Sentiment_Score': [0.882621, 0.826413, 0.893627, 0.863469, 0.982447],
})

# Apply function to dataframe by rows 
df = df.apply(sub_1, axis=1)

apply()之后的df值:

Sentiment_Label  Sentiment_Score
0       Satisfied         0.882621
1    Disappointed         0.173587
2       Satisfied         0.893627
3       Satisfied         0.863469
4    Disappointed         0.017553
3qpi33ja

3qpi33ja4#

你可以使用df.iterrows()循环df。然后你比较列值,并使用df.at()访问值和替换。

for i, v in df.iterrows():
    if(v["Sentiment_Label"] == 'Disappointed'):
        df.at[i, 'Sentiment_Score'] = 1 - v["Sentiment_Score"]

print(df)

输出:

Sentiment_Label  Sentiment_Score
0       Satisfied         0.882621
1    Disappointed         0.173587
2       Satisfied         0.893627
3       Satisfied         0.863469
4    Disappointed         0.017553
puruo6ea

puruo6ea5#

可以使用rsub

m = df['Sentiment_Label'].eq('Disappointed')
df.loc[m, 'Sentiment_Score'] = df['Sentiment_Score'].rsub(1)

输出:

Sentiment_Label  Sentiment_Score
0       Satisfied         0.882621
1    Disappointed         0.173587
2       Satisfied         0.893627
3       Satisfied         0.863469
4    Disappointed         0.017553
rqmkfv5c

rqmkfv5c6#

df.update( df[df.iloc[:,0].eq("Disappointed")].iloc[:,1].sub(1).abs() )

print(df)
Sentiment_Label  Sentiment_Score
0       Satisfied         0.882621
1    Disappointed         0.173587
2       Satisfied         0.893627
3       Satisfied         0.863469
4    Disappointed         0.017553

相关问题