Zanam
Zanam

Reputation: 4807

Pandas subtracting same number from each row of lists

I have pandas dataframes as:

df1['A'].ix[1:3]
2017-01-01 02:00:00    [33, 34, 39]
2017-01-01 03:00:00    [3, 43, 9]

df2['B'].ix[1:3]
2017-01-01 02:00:00    2
2017-01-01 03:00:00    3

I want to perform the following:

difference = df1 - df2

Expected result is:

(df1 - df2).ix[1:3]
2017-01-01 02:00:00    [31, 32, 37]
2017-01-01 03:00:00    [0, 40, 6]

i.e. substract number in df2 from corresponding list in df1

Each list of df1 have same size.

I can't think of better way than list comprehension.

Upvotes: 3

Views: 568

Answers (2)

piRSquared
piRSquared

Reputation: 294508

I can't tell what the objects are in df1. Might be list maybe np.array? IDK?

Better I create my own example

A = pd.Series([[33, 34, 39], [3, 43, 9]])
B = pd.Series([2, 3])

option 1
apply(np.asarray)

A.apply(np.asarray) - B

0    [31, 32, 37]
1      [0, 40, 6]
dtype: object

option 2
uglier but faster, see below

pd.Series((np.array(A.values.tolist()) - B.values[:, None]).tolist(), A.index)

0    [31, 32, 37]
1      [0, 40, 6]
dtype: object

timing

enter image description here

Upvotes: 6

Grr
Grr

Reputation: 16099

You could convert your column values from a list to a numpy array and then do df1.A - df2.B:

df1.A = df1.A.map(np.array)
difference = df1.A - df2.B

Upvotes: 4

Related Questions