Python 如何在数据帧的1个单元格中插入pandas.series对象?
我有这样一个数据帧:Python 如何在数据帧的1个单元格中插入pandas.series对象?,python,pandas,Python,Pandas,我有这样一个数据帧: | col1 | col2 | col3 | -----|--------|--------|--------| 0 | apple | apple | apple | 我有一个pandas.Series对象,看起来像这个[“香蕉”、“土豆”、“西红柿”] 我想在数据帧中创建一个新列,col4,并用空列表,[]或pandas.Series对象填充它。我希望它位于1行(本例中为索引0),但每次执行此操作时,都会出现以下错误: 值的长度与索引
| col1 | col2 | col3 |
-----|--------|--------|--------|
0 | apple | apple | apple |
我有一个pandas.Series
对象,看起来像这个[“香蕉”、“土豆”、“西红柿”]
我想在数据帧中创建一个新列,col4
,并用空列表,[]
或pandas.Series
对象填充它。我希望它位于1行(本例中为索引0),但每次执行此操作时,都会出现以下错误:
值的长度与索引的长度不匹配
所以我认为这是一种尝试:
| col1 | col2 | col3 | col4 |
-----|--------|--------|--------|--------|
0 | apple | apple | apple | banana |
1 | | | | tomato |
2 | | | | potato |
这不是我想要的
我想要的是:
| col1 | col2 | col3 | col4 |
-----|--------|--------|--------|--------------------------------|
0 | apple | apple | apple | ["banana", "tomato", "potato"] |
作为旁注,我已经将列表转换为字符串,但是列的类型是string
,因为我想作为记录上传到Google Big Query
,这对我不起作用
我尝试了很多东西,但都没有成功
有什么想法吗?像这样:
In [649]: df = pd.DataFrame({'col1':['apple'], 'col2':['apple'], 'col3':['apple']})
In [650]: s = pd.Series(["banana", "potato", "tomato"])
In [661]: df['col4'] = [s.tolist()]
In [662]: df
Out[662]:
col1 col2 col3 col4
0 apple apple apple [banana, potato, tomato]