python - 将多维列表中的数据添加到数据框
问题描述
我有一个名为 的二维列表DataList
。此列表的示例如下:
[['Worsheet', 'Field', '', '', 'All Fields', 'Field code', 'Import Sheet', 'Import Column', 'Import Row'], ['Timeliness', 'Requests', '', '', 'Requests', 'A', '1. Timeliness', 'B', '3'], ['Timeliness', '', 'EIRs', '', 'EIRs', 'Ai', '1. Timeliness', 'B', '5'], ['Outcomes', '', 'Granted in full', '', 'Granted in full', 'B', '2. Outcomes', 'B', '7'], ['Exemptions', '', 'S22', '', 'S22', 'S22', '3. Exemptions, exceptions', 'B', '9'], ['Exemptions', '', 'S22A', '', 'S22A', 'S22A', '3. Exemptions, exceptions', 'B', '10'], ['Section 23', '', 'Over 20 days', '', 'Over 20 days', 'Aii', '4. Section 5', 'B', '7']]
如何将每个子列表的第 5 个元素复制到一个新的数据框中,因此预期的输出如下所示:
All Fields
0 Requests
1 EIRs
2 Granted in full
3 S22
4 S22A
5 Over 20 days
解决方案
试试也许:
data=[['Worsheet', 'Field', '', '', 'All Fields', 'Field code', 'Import Sheet', 'Import Column', 'Import Row'], ['Timeliness', 'Requests', '', '', 'Requests', 'A', '1. Timeliness', 'B', '3'], ['Timeliness', '', 'EIRs', '', 'EIRs', 'Ai', '1. Timeliness', 'B', '5'], ['Outcomes', '', 'Granted in full', '', 'Granted in full', 'B', '2. Outcomes', 'B', '7'], ['Exemptions', '', 'S22', '', 'S22', 'S22', '3. Exemptions, exceptions', 'B', '9'], ['Exemptions', '', 'S22A', '', 'S22A', 'S22A', '3. Exemptions, exceptions', 'B', '10'], ['Section 23', '', 'Over 20 days', '', 'Over 20 days', 'Aii', '4. Section 5', 'B', '7']]
df=pd.DataFrame(data[1:], columns=data[0]).iloc[:, 4].to_frame()
输出:
All Fields
0 Requests
1 EIRs
2 Granted in full
3 S22
4 S22A
5 Over 20 days
推荐阅读
- java - 如何从 Uber Cadence 中的 Java 代码获取打开/关闭的工作流列表?
- c++ - 如何在 Windows-10 上的 Visual Studio Code 中设置和配置 MongoDB C/C++ (11/17) 驱动程序
- sql - PL/pgSQL 中的第一次循环后错误游标不存在
- html - 为什么当我增加输入元素按钮的字体大小时没有正确对齐?
- python - 在 python 请求中使用 akamai 到标头时请求未完成
- python - Python:如何按相似键对字典列表进行排序
- python - 我的代码没有看到类变量(对象)?在同一个类中调用不同的函数时
- npm - php artisan nova:tool 时 pngquant 出错
- objective-c - 淡入/淡出 CABasicAnimation & CAGradientLayer
- next.js - ReferenceError:未定义 AudioBuffer