我正在从elasticsearch 中的pandas 数据帧中索引数据。我为某些 es 字段设置了 null_value,但没有为其他字段设置。如何删除没有 null_value 的列,但保留那些具有 null_value 的列(将值设置为 None)?
ES映射:
"properties": {
"sa_start_date": {"type": "date", "null_value": "1970-01-01T00:00:00+00:00"},
"location_name": {"type": "text"},
Run Code Online (Sandbox Code Playgroud)
代码:
cols_with_null_value = ['sa_start_date']
orig = [{
'meter_id': 'M1',
'sa_start_date': '',
'location_name': ''
},{
'meter_id': 'M1',
'sa_start_date': '',
'location_name': 'a'
}]
df = pd.DataFrame.from_dict(orig)
df['sa_start_date'] = df['sa_start_date'].apply(pd.to_datetime, utc=True, errors='coerce')
df.replace({'': np.nan}, inplace=True)
Run Code Online (Sandbox Code Playgroud)
df:
meter_id sa_start_date location_name
0 M1 NaT NaN
1 M1 NaT a
Run Code Online (Sandbox Code Playgroud)
Elasticsearch 索引所需的字典:
{"meter_id": M1, "sa_start_date": None}
{"meter_id": M1, "sa_start_date": None, "location_name": "a"}
Run Code Online (Sandbox Code Playgroud)
注意 带有 …