我的输入是列表列表。我正在尝试将每个子列表转换为具有两列的数据框,然后将所有数据框一起转换。
该列表中大约有80多个子列表,以下仅为示例:
[[('04-24-2019 18:51:54:629', 'a'),
('04-24-2019 18:51:54:790', 'p'),
('04-24-2019 18:51:54:934', 'p'),
('04-24-2019 18:51:55:65', 'l'),
('04-24-2019 18:51:55:157', 'e'),
('04-24-2019 18:51:57:710', 'b'),
('04-24-2019 18:51:57:862', 'o'),
('04-24-2019 18:51:58:501', 'backspace'),
('04-24-2019 18:51:59:368', 'e'),
('04-24-2019 18:51:59:507', 'e'),
('04-24-2019 18:51:59:800', 's'),
('04-24-2019 18:52:05:502', 'backspace')],
[('04-24-2019 18:51:54:629', 'a'),
('04-24-2019 18:51:54:790', 'p'),
('04-24-2019 18:51:54:934', 'p'),
('04-24-2019 18:51:55:65', 'l'),
('04-24-2019 18:51:55:157', 'e'),
('04-24-2019 18:51:57:710', 'b'),
('04-24-2019 18:51:57:862', 'o'),
('04-24-2019 18:51:58:501', 'backspace'),
('04-24-2019 18:51:59:368', 'e'),
('04-24-2019 18:51:59:507', 'e'),
('04-24-2019 18:51:59:800', 's'),
('04-24-2019 18:52:05:502', 'backspace')],
[('04-24-2019 18:51:54:629', 'a'),
('04-24-2019 18:51:54:790', 'p'),
('04-24-2019 18:51:54:934', 'p'),
('04-24-2019 18:51:55:65', 'l'),
('04-24-2019 18:51:55:157', 'e'),
('04-24-2019 18:51:57:710', 'b'),
('04-24-2019 18:51:57:862', 'o'),
('04-24-2019 18:51:58:501', 'backspace'),
('04-24-2019 18:51:59:368', 'e'),
('04-24-2019 18:51:59:507', 'e'),
('04-24-2019 18:51:59:800', 's'),
('04-24-2019 18:52:05:502', 'backspace')]]
org_time_list = []
for x in range(len(data)):
dd = (data[x]["input_clicks"])
org_time_list.append(list(dd.items()))
org_time_list
df_3 = pd.DataFrame()
for x in org_time_list:
for y in x:
df_3.append(pd.DataFrame((y), columns=['Date', 'DateValue']))
df_3
我希望有一个数据框,其中包含仅两列的列表中的所有数据。以下是子列表的示例。
Date DateValue
0 04-24-2019 18:51:54:629 a
1 04-24-2019 18:51:54:790 p
2 04-24-2019 18:51:54:934 p
3 04-24-2019 18:51:55:65 l
4 04-24-2019 18:51:55:157 e
5 04-24-2019 18:51:57:710 b
6 04-24-2019 18:51:57:862 o
7 04-24-2019 18:51:58:501 backspace
8 04-24-2019 18:51:59:368 e
9 04-24-2019 18:51:59:507 e
10 04-24-2019 18:51:59:800 s
11 04-24-2019 18:52:05:502 backspace
答案 0 :(得分:1)
尝试使用这种简单方法:
import functools
import operator
temp_data = functools.reduce(operator.concat, org_time_list) #flatten the list of lists
df_3 = pd.DataFrame(temp_data, columns=['Date', 'DateValue'])
如果您可以显示原始数据的样子,我认为也可以避免第一个循环。