gpt4 book ai didi

python - 创建新列导致 NaN

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:48 24 4
gpt4 key购买 nike

after merging我尝试在 csv 文件中创建一个包含 3 列的新列。但是,如果其中一列值为 NaN ,则整个值更新为 NaN 而不是跳过它。

import pandas as pd
import numpy as np
df = pd.read_csv(r'C:\Users\RAHNA KM\Downloads\Accident_death.csv', skipinitialspace=True)
df['Address']= df["ResidenceCity"].astype(str) +" "+ df["ResidenceCounty"].astype(str) +" "+ df["ResidenceState"]
df.head()

最佳答案

我不确定它为什么这样做,但是用其他东西填充“NaN”可以让你绕过它:

import pandas as pd
import numpy as np
df = pd.read_csv(r'TestData.csv', skipinitialspace=True)
df2 = df.fillna('Unknown')
df2['Address']= df2["ResidenceCity"].astype(str) +" "+ df2["ResidenceCounty"].astype(str) +" "+ df2["ResidenceState"]
df2.head()

关于python - 创建新列导致 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521507/

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