[[[12, 71.2989367, -156.7286143, Samuel Simmonds Memorial Hospital, 4], [4, 64.8311569, -147.7399471, Fairbanks Memorial Hospital, 4].1, [6, 64.4993412, -165.3776787, Norton Sound Regional Hospital, 4].2, [5, 61.56316029999999, -149.2576383, Mat-Su Regional Medical Center, 4]]
需要将其转换为类似数据框上的内容:
ID Lat Long Name Level
12, 71.2989367, -156.7286143, Samuel Simmonds Memorial Hospital, 4
4, 64.8311569, -147.7399471, Fairbanks Memorial Hospital, 4
6, 64.4993412, -165.3776787, Norton Sound Regional Hospital, 4
答案 0 :(得分:0)
非常简单。引用字符串,删除列表中的多余级别。
import pandas as pd
input_data = [[12, 71.2989367, -156.7286143, 'Samuel Simmonds Memorial Hospital', 4],
[4, 64.8311569, -147.7399471, 'Fairbanks Memorial Hospital', 4],
[6, 64.4993412, -165.3776787, 'Norton Sound Regional Hospital', 4],
[5, 61.56316029999999, -149.2576383, 'Mat-Su Regional Medical Center', 4]]
df = pd.DataFrame(input_data,
columns=['ID', 'Lat', 'Long', 'Name', 'Level'])
结果:
ID Lat Long Name Level
0 12 71.298937 -156.728614 Samuel Simmonds Memorial Hospital 4
1 4 64.831157 -147.739947 Fairbanks Memorial Hospital 4
2 6 64.499341 -165.377679 Norton Sound Regional Hospital 4
3 5 61.563160 -149.257638 Mat-Su Regional Medical Center 4
答案 1 :(得分:0)
常规DataFrame
构造函数应该执行此操作,只需记住您的数据中有一些红色.
是三层嵌套列表(因此您需要使用[0]进行访问)
import pandas as pd
data = [[
[12, 71.2989367, -156.7286143, "Samuel Simmonds Memorial Hospital", 4],
[4, 64.8311569, -147.7399471, "Fairbanks Memorial Hospital", 4],
[6, 64.4993412, -165.3776787, "Norton Sound Regional Hospital", 4],
[5, 61.56316029999999, -149.2576383, "Mat-Su Regional Medical Center", 4]
]]
df = pd.DataFrame(data[0], columns=["ID","Lat","Long","Name","Level"])
print(df)
输出:
ID Lat Long Name Level
0 12 71.298937 -156.728614 Samuel Simmonds Memorial Hospital 4
1 4 64.831157 -147.739947 Fairbanks Memorial Hospital 4
2 6 64.499341 -165.377679 Norton Sound Regional Hospital 4
3 5 61.563160 -149.257638 Mat-Su Regional Medical Center 4