首页 > 解决方案 > 将多维列表中的数据添加到数据框

问题描述

我有一个名为 的二维列表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

标签: pythonpandas

解决方案


试试也许:

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

推荐阅读