Python列表到数据框

时间:2018-07-03 14:49:13

标签: python

我想将列表转换为数据框。挑战在于列表中的项目如下,我需要将列表中每个项目的字符串值提取到数据框的列中。

列表看起来像

Keyword = [('Hello',2.0),('Welcome',3.0),('To',3.0),('Python',4.0)]

需要数据框为

输出:

     Keyword  Score
0    Hello    2.0
1    Welcome  3.0
2    To       3.0
3    Python    4.0

有人可以提出一些解决方案吗?

2 个答案:

答案 0 :(得分:4)

使用columns=["Keyword", "Score"]

例如:

import pandas as pd
Keyword = [ ('Hello',2.0),('Welcome',3.0),('To',3.0),('Python',4.0)]
df = pd.DataFrame(Keyword, columns=["Keyword", "Score"])
print(df)

输出:

   Keyword  Score
0    Hello    2.0
1  Welcome    3.0
2       To    3.0
3   Python    4.0

答案 1 :(得分:2)

使用pandas Dataframes可以进行以下操作:

my_dataframe = pd.DataFrame(Keyword, columns=["Keyword", "Score"])

结果如下:

script result

注意::您确实应该考虑使用小写的变量名。