要使列表像列到列一样爆炸,我们可以使用pandas explode()函数。我的熊猫的版本“ 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
答案 0 :(得分:1)
您需要确保您的列是列表类型,才能使用熊猫的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()