【问题标题】:Apply function to return data based on next rows应用函数根据下一行返回数据
【发布时间】:2021-06-14 22:29:23
【问题描述】:

大家好,我有一个数据框 ready_df,如下所示:

timestamp Latitude_1 Longtitude_1 Name Latitude_2 Longtitude_2
2021-03-03 08:00:00.100 NaN NaN NaN 12.3456 12.3456
2021-03-03 08:00:00.300 NaN NaN NaN 12.3456 12.3456
2021-03-03 08:00:00.500 12.3456 12.3456 Billy NaN NaN

我必须用 Latitude_2 Longtitude_2 == Latitude_1 Longtitude_1 的值填充列 Name
我创建了一个函数,我称之为filler(),这是函数的代码:

def filler(Nan_lon, Full_lon, Nan_lat, Full_lat, Designated_col):
    if ready_df[Nan_lon] == ready_df[Full_lon].iloc[1] and ready_df[Nan_lan] == ready_df[Full_lan].iloc[1]:
        return ready_df[Designated_col].iloc[1]
    elif ready_df[Nan_lon] == ready_df[Full_lon].iloc[2] and ready_df[Nan_lan] == ready_df[Full_lan].iloc[2]:
        return ready_df[Designated_col].iloc[2]
    elif ready_df[Nan_lon] == ready_df[Full_lon].iloc[3] and ready_df[Nan_lan] == ready_df[Full_lan].iloc[3]:
        return ready_df[Designated_col].iloc[3]
    else:
        return np.NaN

如果前面的 2 行是 nan,我想检查当前行前面最多 3 行,否则我想返回 nan。但是,当我使用它来填充这样的列时:

ready_df['NAME'] = ready_df.apply(lambda x: filler(Nan_lon=x.Longtitude_2, Full_lon=x.Longtitude_1,
                                                           Nan_lat=x.Latitude_2, Full_lat=x.Latitude_1,
                                                           Designated_col=ready_df['NAME']))

我收到一个错误:AttributeError: 'Series' object has no attribute 'Longitude'

整个想法是填充列Name - 然后我计划更改每列的函数,以便我为每个 NaN 填充正确的值。数据框有 130k 行。您能否解释一下为什么我的功能不起作用并提出任何改进建议,谢谢:)

编辑:

| timestamp | Latitude_1 | Longtitude_1 | Name | Latitude_2 | Longtitude_2 |
| --------- | ---------- | ------------ | ---- | ---------- | ------------ |
| 2021-03-03 08:00:00.100 | NaN | NaN | NaN | 12.3456 | 12.3456 |
| 2021-03-03 08:00:00.300 | NaN | NaN | NaN | 12.3456 | 12.3456 |
| 2021-03-03 08:00:00.500 | 12.3456 | 12.3456 | Billy | NaN | NaN |
| 2021-03-03 08:00:00.700 | 21.345| 21.345| John| NaN | NaN |
| 2021-03-03 08:00:00.900 | 21.345| 21.345| John| NaN | NaN |
| 2021-03-03 08:00:01.100 | 12.3456 | 12.3456 | Billy | NaN | NaN |
| 2021-03-03 08:00:01.300 | NaN | NaN | NaN | 21.345| 21.345|
| 2021-03-03 08:00:01.500 | NaN | NaN | NaN | 21.345| 21.345|
| 2021-03-03 08:00:01.700 | 12.3456 | 12.3456 | Billy | NaN | NaN |  

预期输出:

timestamp Latitude_1 Longtitude_1 Name Latitude_2 Longtitude_2
2021-03-03 08:00:00.100 12.3456 12.3456 Billy 12.3456 12.3456
2021-03-03 08:00:00.300 12.3456 12.3456 Billy 12.3456 12.3456
2021-03-03 08:00:00.500 12.3456 12.3456 Billy 12.3456 12.3456
2021-03-03 08:00:00.700 21.345 21.345 John 21.345 21.345
2021-03-03 08:00:00.900 21.345 21.345 John 21.345 21.345
2021-03-03 08:00:01.100 12.3456 12.3456 Billy 12.3456 12.3456
2021-03-03 08:00:01.300 21.345 21.345 John 21.345 21.345
2021-03-03 08:00:01.500 21.345 21.345 John 21.345 21.345
2021-03-03 08:00:01.700 12.3456 12.3456 Billy 12.3456 12.3456

【问题讨论】:

    标签: python pandas dataframe function apply


    【解决方案1】:

    对此持保留态度,但您可能必须从根本上重新考虑您是如何在这里实现 apply() 的。而且因为我最终不知道你的结果应该是什么,所以其中一些可能与上下文无关。

    1. 像这样使用 apply 只发送一行数据,但您想针对三行进行测试;然后重复每一行的数据帧的长度。您可能需要考虑使用某种切片,而不是在其中迭代数据帧,一次取三行
    2. 但是,您在 x.Longtitude_2 上的错误是因为该语法认为存在 x 的属性,因此使用 x['Longitutde_2'] 会清除该错误,但随后会出现关键字错误,该错误可通过使用参数 axis= 清除1,这导致另一个错误'NAME'未定义。此参数 Designated_col=ready_df['NAME'] 不应包含在 lambda 或函数参数列表中,因为您尝试通过 ready_df['NAME'] = ready_df...创建它。
    3. 除了你传递 Nan_lon=x.Longtitude_2 一个值给你的函数和 ready_df[Nan_lon] 因为它正在寻找 ready_df['12.3456'] 我认为你想要 ready_df['Longtitude_2']
    4. 没有超过这一点......

    如果您可以再发布几行 ready_df 和您预期的输出数据框,您可能会得到更好的建议。

    【讨论】:

    • 嗨乔纳森我刚刚编辑了这个问题,包含了你所问的所有细节,现在我知道 apply() 绝对不是这种东西的方式,谢谢你。看到编辑过的问题后,您有什么建议吗?
    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多