将系列中的单个值分配给数据框列

时间:2018-10-26 15:49:29

标签: python python-3.x pandas dataframe

如何为travel_min列中具有空值的所有3行分配值match_time。travel_min是浮点数。

我收到以下错误:

TypeError:'str'对象不支持项目分配

null_df = data[data['travel_min'].isnull()]
match_time = data.loc[data['train_station_id']==20026]['travel_min']
for row in null_df:
    row['travel_min'] = match_time

1 个答案:

答案 0 :(得分:1)

您可以使用loc提取值,然后使用fillna

Start Outer Try
Start Inner Try
Exception thrown: 'System.Exception' in MyTestApp.exe
In Inner Finally
In outer catch
In outer finally

无需切片数据帧或使用Python级的match_time = data.loc[data['train_station_id']==20026, 'travel_min'].iat[0] df['travel_min'] = df['travel_min'].fillna(match_time) 循环。