gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:31 25 4
gpt4 key购买 nike

我正在尝试添加一个列表作为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com