【发布时间】:2021-02-02 23:52:49
【问题描述】:
基本上,我在这里尝试做的是用名为“lst”的单独列表中的字符串名称/单词填充数据框列名称“Colors”中的缺失值。
在给定代码的情况下,它可以添加到目标行,而其他字符串名称(例如 Green..etc)仍然保留,这很好。
但是,问题在于将整个列表添加到行中,这显然是因为它没有遍历列表中的每个元素。
不确定在这种情况下我将如何处理两个不同长度的变量。嵌套循环会导致超出范围错误。
数据框:
Index Colors
0 nan
1 Red
2 nan
3 Green
4 nan
5 nan
6 Brown
期望的输出:
Index Colors
0 one
1 Red
2 two
3 Green
4 three
5 four
6 Brown
代码:
import pandas as pd
from pandas import np
df_train = pd.DataFrame({'Colors': [np.nan, 'Red', np.nan, 'Green', np.nan, np.nan, 'Brown']})
lst = ['one','two','three','four']
for row in df_train .loc[df_train .file_name.isnull(), 'file_name'].index:
df_train .at[row, 'file_name'] = [i for i in lst]
output of code:
Colors
0 ['one', 'two', 'three', 'four']
1 Red
2 ['one', 'two', 'three', 'four']
3 Green
4 ['one', 'two', 'three', 'four']
5 ['one', 'two', 'three', 'four']
6 Brown
【问题讨论】: