gpt4 book ai didi

Python - 迭代行和列

转载 作者:行者123 更新时间:2023-12-01 01:52:31 24 4
gpt4 key购买 nike

首先请原谅我的技术。我正在尝试进入Python,我学习只是为了好玩,比如说,我不专业地使用它,而且说实话,我很糟糕。我的问题可能会有基本错误。

无论如何,我正在尝试检查数据框的行和列。我想检查列的值(第一列除外)是否为 NaN。如果是,那么它们应该更改为第一个的值。

import math

for index, row in rawdata3.iterrows():
test = row[0]
for column in row:
if math.isnan(row.loc[column]) == True:
row.loc[column] = test

我得到的错误是这样的:

the label [4.0] is not in the [columns]

我还遇到了其他错误,代码略有不同,例如:

cannot do label indexing on class pandas.core.indexes.base.Index with these indexers class float

你能帮我一下吗?

提前致谢!

干杯。

最佳答案

我不知道是否有更好的方法,但这工作正常:

for i in df.columns:
df.loc[df[i].isnull(), i] = df.loc[df[i].isnull(), 'A']

输出:

   A    B    C
0 5 5.0 2.0
1 6 5.0 6.0
2 9 9.0 9.0
3 2 4.0 6.0

关于Python - 迭代行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50570625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com