如何将表转换为数组

问题描述 投票:0回答:1

我有如下的表格

| item_name | item_value | timestamp |
|:---- |:------:| -----:|
|  A | 0.25    | 2023-03-01T17:20:00.000+0000 |
|  B | 0.34    | 2023-03-01T17:20:00.000+0000 |
|  A | 0.30    | 2023-03-01T17:30:00.000+0000 |
|  B | 0.54    | 2023-03-01T17:30:00.000+0000 |
|  A | 0.32    | 2023-03-01T17:35:00.000+0000 |
|  B | 0.67    | 2023-03-01T17:35:00.000+0000 |

如何转换成

 [[0.25, 0.34], [0.30,0.54],[0.32,0.67]]

任何帮助将不胜感激!

python pyspark databricks
1个回答
0
投票

给定:

table = """| item_name | item_value | timestamp |
|:---- |:------:| -----:|
|  A | 0.25    | 2023-03-01T17:20:00.000+0000 |
|  B | 0.34    | 2023-03-01T17:20:00.000+0000 |
|  A | 0.30    | 2023-03-01T17:30:00.000+0000 |
|  B | 0.54    | 2023-03-01T17:30:00.000+0000 |
|  A | 0.32    | 2023-03-01T17:35:00.000+0000 |
|  B | 0.67    | 2023-03-01T17:35:00.000+0000 |"""

你需要做的是:

按换行符拆分表格

rows = table.splitlines()

然后遍历表的行并用 | 拆分每一行,将第 3 位的项目取下来,然后将其转换为 float

[
    [
        float(row_A.split("|")[2].strip()), 
        float(row_B.split("|")[2].strip())
    ]
    for row_A, row_B in zip(rows[2::2],rows[3::2])
]
© www.soinside.com 2019 - 2024. All rights reserved.