如果另一列的同一行中的值也为空,我想将一列的值设置为null(np.nan)。
我正在尝试:
df.loc[df[7].isnull(), df[10]] = np.nan
我收到错误消息:
KeyError: "None of [Index(['Density at 15 C (g/mL) (ASTM D5002)', 'Method', 'ASTM D5002)',\n 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)',\n 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)',\n ...\n 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)',\n 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)', 'ASTM D5002)',\n 'ASTM D5002)', 'ASTM D5002)'],\n dtype='object', length=297)] are in the [columns]"
我尝试通过使用以下命令删除“空白”,正如我在另一个答案中所看到的那样:
delimiter=','
并尝试过:
df[7]=df[7].replace(' ','')
我仍然遇到相同的错误。
答案 0 :(得分:2)
使用:
df.loc[df[7].isnull(),10] = np.nan
示例:
df=pd.DataFrame()
df[1]=[np.nan, 2 ,3 ,4, np.nan]
df[2]=[1,2,3,4,5]
print(df)
1 2
0 NaN 1
1 2.0 2
2 3.0 3
3 4.0 4
4 NaN 5
df.loc[df[1].isnull(),2]=np.nan
print(df)
1 2
0 NaN NaN
1 2.0 2.0
2 3.0 3.0
3 4.0 4.0
4 NaN NaN
其他情况:
df=pd.DataFrame()
df[1]=[np.nan, 2 ,3 ,4, '']
df[2]=[1,2,3,4,5]
print(df)
1 2
0 NaN 1
1 2 2
2 3 3
3 4 4
4 5
df.loc[df[1].isnull(),2]=np.nan
print(df)
1 2
0 NaN NaN
1 2 2.0
2 3 3.0
3 4 4.0
4 5.0
正如您在此处看到的那样,您无法仅使用isull进行检查
在这种情况下,您需要:
df.loc[df[1].isnull()|df[1].eq(''),2]=np.nan
print(df)
1 2
0 NaN NaN
1 2 2.0
2 3 3.0
3 4 4.0
4 NaN