解包功能返回到pandas数据框列 [英] unpacking function return into pandas dataframe columns

查看:128
本文介绍了解包功能返回到pandas数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据框("radar_locations"),其中包含(除其他事项外)纬度和经度坐标.对于该信息,我需要添加一个国家和州列,因此我编写了一个函数,该函数执行反向地理编码并返回所需的两个值return geodata.state, geodata.country

I have an existing dataframe ("radar_locations") which contains (among other things) latitude and longitude coordinates. To that information, I need to add a country and state column, so I've written a function that does a reverse geocode and returns the two values needed return geodata.state, geodata.country

当我尝试将值分配到数据框中的新列时,出现错误,提示要解包的值太多.但是,如果我更新代码以使函数返回单个值,则可以将该值成功写入新的dataframe列中.

When I try to assign the values into new columns in the dataframe, I get an error that there are too many values to unpack. But if I update the code so that the function returns a single value, I can successfully write that value into a new dataframe column.

如果这只是大熊猫的怪癖,还是我更想念一些更基本的东西?

If this just an eccentricity of pandas or is there something more fundamental I'm missing?

有效

def reverse_geocode(lat, long):
    ...
    return geodata.country

radar_locations['Country'] = radar_locations.apply(lambda x: reverse_geocode(x[1], x[0]), axis=1)

有效

def reverse_geocode(lat, long):
    ...
    return geodata.state, geodata.country

state, country = reverse_geocode(mylat, mylong)

失败

def reverse_geocode(lat, long):
    ...
    return geodata.state, geodata.country

radar_locations['State'], radar_locations['Country'] = radar_locations.apply(lambda x: reverse_geocode(x[1], x[0]), axis=1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-82e3c63a2ecb> in <module>()
     19         raise
     20 
---> 21 radar_locations['State'], radar_locations['Country'] =   radar_locations.apply(lambda x: reverse_geocode(x[1], x[0]), axis=1)

ValueError: too many values to unpack (expected 2)

推荐答案

使用zip*运算符解压缩并执行分配:

Use zip and the * operator to unzip and perform the assignment:

# A function that returns multiple things.
def some_func(x):
    return x+1, x+2

# Example DataFrame
df = pd.DataFrame({'A': range(5)})

# Example usage.
df['B'], df['C'] = zip(*df['A'].apply(some_func))

结果输出:

   A  B  C
0  0  1  2
1  1  2  3
2  2  3  4
3  3  4  5
4  4  5  6

尝试直接从apply进行分配的问题是,当您返回多个值时,实际上是在返回一个元组列,而不是两个单独的列,这就是为什么需要进行解压缩的原因:

The issue with trying to assign directly from the apply is that when you return multiple values, you're actually returning a single column of tuples, not two separate columns, which is why the unzipping process is necessary:

df['A'].apply(some_func)

0    (1, 2)
1    (2, 3)
2    (3, 4)
3    (4, 5)
4    (5, 6)

这篇关于解包功能返回到pandas数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆