python - 我有一个列表,我希望列表中的每个元素都作为一行

标签 python dataframe

我有一个列表列表,我想将每个列表分配给特定列,我已经创建了数据框的列。但在每一列中,元素都以列表的形式出现。我希望此列表中的每个元素都是单独的行,作为该特定列的一部分。

这就是我所做的:

df = pd.DataFrame([np.array(dataset).T],columns=list1) 

print(df)

附上输出的屏幕截图。 1

我希望该列表的每个元素都是一行,作为我的输出。

最佳答案

这应该可以为您完成工作:

import pandas as pd

Fasteners = ['Screws & Bolts', 'Threaded Rods & Studs', 'Eyebolts', 'U-Bolts']
Adhesives_and_Tape = ['Adhesives','Tape','Hook & Loop']
Weld_Braz_Sold = ['Electrodes & Wire','Gas Regulators','Welding Gloves','Welding Helmets & Glasses','Protective Screens']

df = pd.DataFrame({'Fastener': pd.Series(Fasteners), 'Adhesives_and_Tape': pd.Series(Adhesives_and_Tape), 'Weld_Braz_Sold': pd.Series(Weld_Braz_Sold)})

print(df)

请提供您所起始的数据库的结构或相应列表的结构。然后我可以针对您的具体问题为您提供更有针对性的答案。

如果结构变得越来越大,您还可以在生成数据框时迭代所有列表。这只是解决您问题的基本流程。

请随时发表评论以获取进一步帮助。

编辑

如果您想循环遍历列表数据库。另外使用以下代码:

for i in range(len(list1)): df.iloc[:,i] = pd.Series(dataset[i])

关于python - 我有一个列表,我希望列表中的每个元素都作为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55392985/

相关文章:

python - 如何从http响应中解析这个json.dumps并访问每个值?

python - 如何根据日期时间差异拆分数据框?

python - 数据错误 : No numeric types to aggregate

r - 使用对象而不是数组将 R 数据帧嵌套到 JSON

jquery - Django-cors-headers 不工作

python - Scrapy response.replace编码错误

python - 为什么 PyInstaller 没有检测到请求模块?

python - 是否可以阻止 python 终止直到 block 结束?

python - 用多索引迭代连接 Pandas 数据框

python - 从 Pandas DataFrame 创建复杂的嵌套字典