Reputation: 663
I have a dataframe like this:
| col1 | col2 | col3 |
-----|--------|--------|--------|
0 | apple | apple | apple |
And I have a pandas.Series
object that looks like this ["banana", "potato", "tomato"]
I want to create a new column, col4
, into the dataframe and either fill it with an empty list, []
, or the pandas.Series
object. I want it to be on 1 row (index 0 in this case) but everytime I do this I get the following error:
Length of values does not match length of index
So I think it's trying something like:
| col1 | col2 | col3 | col4 |
-----|--------|--------|--------|--------|
0 | apple | apple | apple | banana |
1 | | | | tomato |
2 | | | | potato |
which is not what I want.
What I want is:
| col1 | col2 | col3 | col4 |
-----|--------|--------|--------|--------------------------------|
0 | apple | apple | apple | ["banana", "tomato", "potato"] |
As a sidenote I already did it by converting the list to string but then the column is of type string
, and since I want to upload to Google Big Query as a RECORD
it doesn't work for me.
I tried many things and none worked.
Any ideas?
Upvotes: 0
Views: 883
Reputation: 34056
Like this:
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]
Upvotes: 1