[英]How To Replace Pandas Column NaN Values with Empty List Values?
Given a json
string of records where the "schema" for each record is not consistent (eg each record does not have the full set of "columns"):给定一个
json
记录字符串,其中每条记录的“模式”不一致(例如,每条记录都没有完整的“列”集):
s = """[{"a": 3, "b":[]}, {"a": 4, "b": [4]}, {"a": 5}]"""
A pandas DataFrame
can be constructed from this string:可以从这个字符串构造一个 pandas
DataFrame
:
import pandas as pd
import json
json_df = pd.DataFrame.from_records(json.loads(s))
Which results in这导致
a b
0 3 []
1 4 [4]
2 5 NaN
How can all NaN
instances of a pandas Series
column be filled with empty list
values?如何用空
list
值填充熊猫Series
列的所有NaN
实例? The expected resulting DataFrame would be:预期的结果 DataFrame 将是:
a b
0 3 []
1 4 [4]
2 5 []
I have tried the following;我尝试了以下方法; none of which worked:
没有一个工作:
json_df[json_df.b.isna()] = [[]]*json_df[json_df.b.isna()].shape[0]
from itertools import repeat
json_df[json_df.b.isna()] = repeat([], json_df[json_df.b.isna()].shape[0])
import numpy as np
json_df[json_df.b.isna()] = np.repeat([], json_df[json_df.b.isna()].shape[0])
Thank you in advance for your consideration and response.预先感谢您的考虑和回应。
first find the nan and replace by the same shape of data首先找到nan并替换为相同形状的数据
json_df.loc[json_df.b.isnull(), 'b'] = json_df.loc[json_df.b.isnull(), 'b'].apply(lambda x: [])
a b
0 3 []
1 4 [4]
2 4 []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.