Code Sample:
df = DataFrame({'a': range(10), 'b': range(20, 30), 'c': range(30, 40)})
df.apply(lambda x: [x])
out[]:
a [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
b [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
c [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Problem description
Apply get same result for each columns, I think it should not!
Expected Output
a [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] b [[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]] c [[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]]
Output of pd.show_versions()
Comment From: jorisvandenbossche
There is certainly something strange going on, but what are you exactly trying to obtain? Because now you are trying to put Series objects inside a list inside a dataframe. But does not seem that useful.
Comment From: xmduhan
I have a datafrme present some parameters:
p1 p2 p3
a 0 0 0
b 1 1 1
c 2 2 2
... ...
each row is a group of a parameters. I will do some calculation with parameters, then I will get some results, a value and series. I need to save them on datafrme:
p1 p2 p3 value series
a 0 0 0 0 [...]
b 1 1 1 1 [...]
c 2 2 2 2 [...]
It's fine to do it row by row. But the calculation is time consuming, and can it be optimizated with whole dataframe calculating at once. And then batch result will be a series present each row's value, and result dataframe with each row's series. Result dataframe's columns is parameter dataframe's index:
a b c
0 0 20 30
1 1 21 31
2 2 22 32
3 3 23 33
4 4 24 34
5 5 25 35
I want to put them back. I use:
resultSeries = resultDataframe.apply(lambda x: [x])
to change it to a series, then use:
parameterDataframe['series'] = resultSeries
to put them back.
But the "resultDataframe.apply(lambda x: [x])" does not get what I need.
Comment From: BranYang
df.apply(lambda x: [list(x.values)])
will do what you want @xmduhan
In [4]: df.apply(lambda x: [list(x.values)])
Out[4]:
a [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
b [[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
c [[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]]
dtype: object
Comment From: jreback
as @BranYang example shows. You are just returning a reference to the variable. Using lists as cell should simply be avoided as its completely non-performant (and you are using something mutable anyhow).