gpt4 book ai didi

python - 你如何在第一行第一列中输入 NaN 值?

转载 作者:行者123 更新时间:2023-12-05 09:28:54 24 4
gpt4 key购买 nike

import pandas as pd
data=['a',2,],['b',4,1],['c',6,],['d',4.4,]
df = pd.DataFrame(data, columns = ['Name', 'Age','number'])
df
Name Age number
0 a 2.0 NaN
1 b 4.0 1.0
2 c 6.0 NaN
3 d 4.4 NaN

我想用 NaN 值替换“a”(第一行第一列)

出现这个错误

data=[,2,],['b',4,1],['c',6,],['d',4.4,]
^
SyntaxError: invalid syntax

我试过这个

data=['',2,],['b',4,1],['c',6,],['d',4.4,]
df = pd.DataFrame(data, columns = ['Name', 'Age','number'])
df

Name Age number
0 2.0 NaN
1 b 4.0 1.0
2 c 6.0 NaN
3 d 4.4 NaN

然后我检查了缺失值,显然它没有在第 1 行第 1 列中提取空值

na= [features for features in df.columns if df[features].isnull().sum()>0]
na

['number']

有关如何解决此问题的任何建议。谢谢你

最佳答案

Pandas 是用 numpy 构建的,所以只需使用 np.nan:

import numpy as np

data=[np.nan,2,],['b',4,1],['c',6,],['d',4.4,]
df = pd.DataFrame(data, columns = ['Name', 'Age','number'])
print (df)

输出:

  Name  Age  number
0 NaN 2.0 NaN
1 b 4.0 1.0
2 c 6.0 NaN
3 d 4.4 NaN

关于python - 你如何在第一行第一列中输入 NaN 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70873600/

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