要将类似列表的列分解为行,可以使用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
您需要确保您的列是列表类型,以便能够使用pandas
explode()
。以下是一个可行的解决方案:要一次分解多个列,可以执行以下操作:
相关问题 更多 >
编程相关推荐