亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

熊貓爆炸功能不適用于字符串列列表

熊貓爆炸功能不適用于字符串列列表

Smart貓小萌 2023-06-06 16:11:51
要從列到行分解列表,我們可以使用 pandas?explode()函數。我的熊貓'版本'?0.25.3?'給定的示例對我有用,Stackoverflow.com 的另一個答案按預期工作,但不適用于我的數據集。? ? city? ? ? ? nested_city0? ?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
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

您需要確保您的列是列表類型才能使用 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()



查看完整回答
反對 回復 2023-06-06
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號