Pandas explode函数不适用于字符串列列表

2024-10-03 13:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

要将类似列表的列分解为行,可以使用pandasexplode()函数。我的熊猫“版本”0.25.3

给定的example对我有效,Stackoverflow.com的另一个答案如预期一样有效,但对我的数据集无效

    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

Tags: testcitydataindexsantajannesteddaniel
1条回答
网友
1楼 · 发布于 2024-10-03 13:31:27

您需要确保您的列是列表类型,以便能够使用pandas explode()。以下是一个可行的解决方案:

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()

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

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()

相关问题 更多 >