在Pandas中,根据列表或系列中的值,改变与条件匹配的列中的行的值

2024-04-24 05:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据两个条件选择行

df.Length.str.isnumeric() == False & df.Type == "Type1"

并将特定列Length中所有对应行的值更改为列表中的值,例如:

[120, 2151, 215, 25, 2451]

谢谢你!你知道吗


Tags: falsedf列表type条件lengthstrtype1
1条回答
网友
1楼 · 发布于 2024-04-24 05:48:27

编辑

根据你的评论,我想出了一个简短的解决方案。你知道吗

import pandas as pd
import math

df = pd.DataFrame(
                [
                [10, 15, float('NaN')],
                [25, 30, 35],
                [40, 45, 50],
                [55, 60, float('NaN')]
                ], columns=list('ABC'))

pre_computed_list = [20, 65]


row_indices = df['C'][df['C'].apply(math.isnan)].index.tolist()
df['C'][row_indices] = pre_computed_list

相关问题 更多 >