【发布时间】:2020-05-07 12:00:09
【问题描述】:
我的 pandas 库有问题...我使用 pandas.to_numeric 将列与一个值进行比较,但是,当原始值不是“数字相似”时,pd.to_numeric 将 csv 单元格转换为一个“nan”值,但我不知道如何在“if”中比较这个“nan”值...... 基本上,如果单元格的值为 nan,我需要将单元格捕捉到正确的位置,如下所示:
while counter < len(df): #df is my pandas Dataframe
if df.values[counter][2].isnull(): #[2] is my column with "possible nan values, but not works!"
if 0 <= df.values[counter][4] <= 100: #[4] is my column with "number values"
df.values[counter][2] = df.values[counter][4]
df.values[counter][1] = df.values[counter][3] #[3] Like column [4]
counter += 1
基本上,我必须这样做的真正原因是因为我的 CSV 文件有时会在某些行中出现“向右移动”!
提前致谢!!
@编辑 1:
我的数据集:
时间 | ... |当前 | bt_disable | bt_cpy | bt_health | ...
2561456 | ... | 48945 |正常 | 20 |好 | ...
2589749 | ... | 41485 |正常 | 21 |好 | ...
2561456 | ... | 48945 |正常 | 22 |好 | ...
2561986 | ... | 1 | 48515 |南 | 23 | ...
2569921 | ... | 1 | 49821 |南 | 24 | ...
2561456 | ... | 43253 | 0 | 25 |好 | ...
粗体中的两条线已经向右移动了... “换档”是自动的,我无法控制……
to_numeric 行:
df[BCY] = pd.to_numeric(df[BCY], errors='coerce') #BCY and others is the name of column
df[BCN] = pd.to_numeric(df[BCN], errors='coerce')
df[BPD] = pd.to_numeric(df[BPD], errors='coerce')
df[BHT] = pd.to_numeric(df[BHT], errors='coerce')
您可以看到我的原始 CSV 文件在 | 中有列... |我不使用,但是,当我在 BT_CPY 列中使用 to_numeric 并且行移动时,值 normal 转换为“nan”,我需要的是验证这个 nan,如果nan == true 我需要捕获 bt_health 值并放入 bt_cpy 列!
【问题讨论】:
-
你能告诉我们你的数据集吗?
-
@Shrey 当然,等一下!
标签: python pandas csv type-conversion