python - 将列表添加为 pandas Dataframe 中的值

标签 python list pandas dictionary dataframe

我正在尝试添加一个列表作为 Pandas 中的值:

df = pd.DataFrame() 

temp = {'index' : '15054',
        'Titel': 'Tsjechisch',
        'Woorden': '1314',
        'Secties': '5',
        'Namen_Secties': [u'== Alfabet ==\n', u'== Structuur ==\n', u'== Geschiedenis van het Tsjechisch ==\n', u'== Zie ook ==\n', u'== Externe links ==\n']
        }

pd.DataFrame(temp)

问题如下所示,列表中的每个变量都作为新行打印在我的数据框中。我实际上希望这个数据框只是一行,列表作为列中的一个变量名称_secties

    Names_Secties                      Secties  Title   Words       index
0   == Alfabet ==\n                      5  Tsjechisch  1314        15054
1   == Structuur ==\n                    5  Tsjechisch  1314        15054
2   == Geschiedenis van het Tsjechisch ==\n     5   Tsjechisch  1314    15054
3   == Zie ook ==\n                      5  Tsjechisch  1314        15054
4   == Externe links ==\n                5  Tsjechisch  1314    15054

最佳答案

嵌套列表需要另一个[]:

temp = {'index' : '15054',
        'Titel': 'Tsjechisch',
        'Woorden': '1314',
        'Secties': '5',
        'Namen_Secties': [[u'== Alfabet ==\n', u'== Structuur ==\n']]
        }
df = pd.DataFrame(temp)
print (df)
                          Namen_Secties Secties       Titel Woorden  index
0  [== Alfabet ==\n, == Structuur ==\n]       5  Tsjechisch    1314  15054

关于python - 将列表添加为 pandas Dataframe 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42809814/

相关文章:

python - 如何测试一个列表是否包含另一个包含 Python 中特定项目的列表?

python - 使用具有不同大小值的索引创建 pandas 数据框

python - 如何在通过 Ctrl-v 选择的一堆行上调用函数?

python - pocketsphinx 的正确导入查询是什么?

python - 模拟掷骰子游戏

c# - 列表中的 StackoverflowException

python - Pycharm - 安装软件包

c# - 如何使用 LINQ 从 List<Price> 中获取最接近的数字?

python - 具有初始值的 Pandas cumsum

python - 有条件地过滤一个 df 中的行以获取另一个 df 的子集共有的特定列