在python中,如何使单元格之间没有括号的Pandas列表?

2024-05-23 23:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我从阅读gsheet中得到了一个列表,所有值都放在方括号中,如下所示:

[['example1'], ['example2'], ['example3'], ['example4'], ['example5']]

我想从值中删除括号,以便采取进一步的步骤,它应该如下所示:

['example1', 'example2', 'example3', 'example4', 'example5']

我被困在这里有一段时间了,非常感谢您的帮助。 我的代码:

    SOURCE_SHEET = CLIENT.open_by_url(SOURCE_G_SHEET_URL).worksheet(source_ws_title)
    rows_read = SOURCE_SHEET.get_all_records()
    df_raw = pd.DataFrame(rows_read)
    list = df_raw.values.tolist()

Tags: sourcedf列表readrawsheet括号rows
2条回答

如果列表中的每个列表只有一条记录,您也可以执行此列表理解:

[i[0] for i in initial_list]

您需要的是从内部列表中提取每个元素并将其放入主列表中,如下所示:

[j for i in init_list for j in i]

它是如何工作的?可以将其视为一种外观:

final_list = []
for i in init_list:
    for j in i:
        final_list.append(j)

另一种方法是使用numpysqueeze函数

x = np.array(init_list)
np.squeeze(x)

相关问题 更多 >