pandas 爆炸功能不适用于字符串列列表 [英] Pandas explode function not working for list of string column

查看:45
本文介绍了 pandas 爆炸功能不适用于字符串列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要像列到行一样分解列表,我们可以使用pandas explode() 函数.我的熊猫版本0.25.3"

To explode list like column to row, we can use pandas explode() function. My pandas' version '0.25.3'

给定的示例 对我有用,Stackoverflow.com 的另一个答案按预期工作,但对我的数据集不起作用.

The given example worked for me and another answer of Stackoverflow.com works as expected but it doesn't work for my dataset.

    city        nested_city
0   soto        ['Soto']
1   tera-kora   ['Daniel']
2   jan-thiel   ['Jan Thiel']
3   westpunt    ['Westpunt']
4   nieuwpoort  ['Nieuwpoort', 'Santa Barbara Plantation']

我尝试过的:

test_data['nested_city'].explode()

test_data.set_index(['nested_city']).apply(pd.Series.explode).reset_index()

输出

0    ['Soto']                                  
1    ['Daniel']                                
2    ['Jan Thiel']                             
3    ['Westpunt']                              
4    ['Nieuwpoort', 'Santa Barbara Plantation']
Name: neighbors, dtype: object

推荐答案

您需要确保您的列是列表类型才能使用 pandas 的 explode().这是一个有效的解决方案:

You need to ensure that your column is of list type to be able to use pandas' explode(). Here is a working solution:

from ast import literal_eval

test_data['nested_city'] = test_data['nested_city'].apply(literal_eval) #convert to list type
test_data['nested_city'].explode()

要一次分解多个列,您可以执行以下操作:

To explode multiple columns at a time, you can do the following:

not_list_cols = [col for col in test_data.columns if col not in ['col1', 'col2']] #list of columns you are not exploding (assume col1 and col2 are being exploded)
test_data = test_data.set_index(not_list_cols).apply(pd.Series.explode).reset_index()

这篇关于 pandas 爆炸功能不适用于字符串列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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