【问题标题】:Add List as value in pandas Dataframe在熊猫数据框中添加列表作为值
【发布时间】:2017-03-15 12:31:03
【问题描述】:

我正在尝试在 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)

问题在于,如下所示,列表中的每个变量都被打印为我的数据框中的新行。我实际上希望这个数据框只有一行,列表作为列 names_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

【问题讨论】:

    标签: python list pandas dictionary dataframe


    【解决方案1】:

    嵌套列表需要另一个[]

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-24
      • 2018-12-19
      • 1970-01-01
      • 2019-01-02
      • 2015-01-04
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多