假设我有以下 python 列表,其中包含一些随机字符串和多个 nan 值:
l = [1, 'str', ',,,,', math.nan, 'another_str', math.nan, math.nan, 'last_str']
我想要实现的是一个字典,其中上述列表中元素的索引作为键,元素本身作为值,看起来像这样:
{0: 1,
1: 'str',
2: ',,,,',
3: nan,
4: 'another_str',
5: nan
6: nan
7: 'last_str'}
以下代码:
d_l = {}
for i in l:
idx = l.index(i)
print(idx, i)
d_l[idx] = i
退货
0 1
1 str
2 ,,,,
3 nan
4 another_str
3 nan
3 nan
7 last_str
所以我们看到nan值的索引已经被‘回收’了。这是非常合乎逻辑的,因为 nan 值彼此相同。
为了避免这种情况,我尝试用唯一的字符串替换 nan 值:
d_l = {}
for i in l:
idx = l.index(i)
# check if i is nan, replace for unique str if that's the case
if pd.isna(i):
print(f"{i} is nan, index is {idx}")
i = f"{idx} to be replaced"
else:
pass
d_l[idx] = i
返回:
nan is nan, index is 3
nan is nan, index is 3
nan is nan, index is 3
{0: 1, 1: 'str', 2: ',,,,', 3: '3 to be replaced', 4: 'another_str', 7: 'last_str'}
我还用
if pd.isna(i)
替换了 if el is np.nan
,但我得到了完全相同的结果。