【问题标题】:Fill NA with another list pandas用另一个列表 pandas 填充 NA
【发布时间】: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

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    尝试分配

    df.loc[df.Colors.isnull(),'Colors'] = lst 
    df
    Out[296]: 
       Index Colors
    0      0    one
    1      1    Red
    2      2    two
    3      3  Green
    4      4  three
    5      5   four
    6      6  Brown
    

    【讨论】:

    • 感谢@BENY 允许我在 5 分钟内接受答案。
    猜你喜欢
    • 2022-08-22
    • 2019-07-27
    • 2021-12-26
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多