为了像列对行那样爆炸列表,我们可以使用熊猫爆炸()函数。我的熊猫版“0.25.3”
给定的示例对我有效,而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发布于 2020-08-26 01:53:28
您需要确保您的列是列表类型,才能使用熊猫的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()https://stackoverflow.com/questions/63472664
复制相似问题