在python中,如何使单元格之间没有括号的熊猫列表?
我从阅读gsheet中得到了一个列表,所有值都放在方括号中,如下所示:在python中,如何使单元格之间没有括号的熊猫列表?,python,pandas,list,Python,Pandas,List,我从阅读gsheet中得到了一个列表,所有值都放在方括号中,如下所示: [['example1'], ['example2'], ['example3'], ['example4'], ['example5']] [j for i in init_list for j in i] 我想从值中删除括号,以便采取进一步的步骤,它应该如下所示: ['example1', 'example2', 'example3', 'example4', 'example5'] 我被困在这里有一段时间了,非常
[['example1'], ['example2'], ['example3'], ['example4'], ['example5']]
[j for i in init_list for j in i]
我想从值中删除括号,以便采取进一步的步骤,它应该如下所示:
['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()
您需要的是从内部列表中提取每个元素并将其放入主列表中,如下所示:
[['example1'], ['example2'], ['example3'], ['example4'], ['example5']]
[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)
另一种方法是使用numpy
的压缩功能
x = np.array(init_list)
np.squeeze(x)
如果列表中的每个列表只有一条记录,您也可以执行此列表理解:
[i[0] for i in initial_list]
旁注:避免使用list
作为变量名,因为这会掩盖另一个旁注中内置的list
注意:我假设您希望将工作表从Google Sheets加载到数据框中。我真的很喜欢Pygsheets的这个功能,它正好做到: