python - 从熊猫数据框中删除嵌套列
问题描述
我有一个包含嵌套数据的规范化数据框。
timestamp temp weather
0 08/01/2021 00:00:00 17.82 [{'id': 800, 'main': 'Clear', 'description':...
1 08/01/2021 01:00:00 17.12 [{'id': 800, 'main': 'Clear', 'description':...
2 08/01/2021 02:00:00 16.53 [{'id': 800, 'main': 'Clear', 'description':...
我已使用以下方法将嵌套数据提取到另一个数据框中:
df_history_small = pd.json_normalize(data_history['hourly'], record_path='weather',meta=['dt','temp', 'humidity'], errors='ignore')
像这样我得到:
id main description icon dt temp humidity
0 800 Clear clear sky 01n 08/01/2021 00:00:00 17.82 69
1 800 Clear clear sky 01n 08/01/2021 01:00:00 17.12 72
2 800 Clear clear sky 01n 08/01/2021 02:00:00 16.53 74
现在我想删除列“主要”、描述和图标,但出现错误:
df_history_small = df_history_small.drop([('main', 'description', 'icon')], axis=1, inplace=True)
我怎样才能删除这些列。
谢谢
解决方案
drop
按照@ALollz 的建议使用和修复您的语法:
>>> df.drop(['main', 'description', 'icon'], axis='columns')
id dt temp humidity
0 800 08/01/2021 00:00:00 17.82 69
1 800 08/01/2021 01:00:00 17.12 72
2 800 08/01/2021 02:00:00 16.53 74
推荐阅读
- python - 如何获取两个子字符串之间的字符串?
- reactjs - Highcharts列动画问题
- reactjs - 在 windows 中安装 babel
- angular - 是否可以在多个位置显示角度组件的单个实例?
- ios - OutputStream.write 不会发送完整的数据,而只会发送固定的数量
- javascript - 如何通过中间网站将用户重定向到第三方网站
- android - READ_PHONE_NUMBERS 在 android O 下不工作
- report - Odoo 10 Qweb PDF 报告中自定义检查页面上的分页问题
- python - 在 python 中抓取电子商务 - 无法获取产品类别和总金额
- amazon-web-services - 是否可以在使用 s3-dist-cp 时指定 mappers-reducer 的数量?