列出了加载到pandas dataframe df2
中的人员的属性。对于清理,我想将值0
或'0'
替换为np.nan
。
df2.dtypes
ID object
Name object
Weight float64
Height float64
BootSize object
SuitSize object
Type object
dtype: object
使用代码将值0设置为np.nan
:
df2.loc[df2['Weight'] == 0,'Weight'] = np.nan
df2.loc[df2['Height'] == 0,'Height'] = np.nan
df2.loc[df2['BootSize'] == '0','BootSize'] = np.nan
df2.loc[df2['SuitSize'] == '0','SuitSize'] = np.nan
相信这可以用类似/更短的方式完成:
df2[["Weight","Height","BootSize","SuitSize"]].astype(str).replace('0',np.nan)
但是上述方法不起作用。零保留在df2中。如何解决这个问题?
答案 0 :(得分:21)
我认为dict
需要replace
:
cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].replace({'0':np.nan, 0:np.nan})
答案 1 :(得分:3)
您可以使用'replace'方法并将要替换的值作为第一个参数传递给列表,并将所需的值作为第二个参数传递给
cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].replace(['0', 0], np.nan)
答案 2 :(得分:1)
data['amount']=data['amount'].replace(0, np.nan)
data['duration']=data['duration'].replace(0, np.nan)
答案 3 :(得分:1)
尝试:
df2.replace(to_replace={
'Weight':{0:np.nan},
'Height':{0:np.nan},
'BootSize':{'0':np.nan},
'SuitSize':{'0':np.nan},
})
答案 4 :(得分:0)
另一种方法:
cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].mask(df2[cols].eq(0) | df2[cols].eq('0'))
答案 5 :(得分:0)
在“年龄”列中,用空格替换零
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
单列用nan替换零
public function renderAddForm()
{
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Impostazione App Mobile'),
),
'input' => array(
array(
'type' => 'categories',
'label' => $this->l('Seleziona Categoria'),
'name' => 'name',
'tree' => array(
'id' => 'category',
'name' => 'name',
'selected_categories' => array((int)Configuration::get('category')),
'use_checkbox' => false
)
),
多列用 nan 替换零
df['age'].replace(['0', 0'], '', inplace=True)
用 nan 替换数据框的零
df['age'] = df['age'].replace(0, np.nan)