Python Pandas: Using apply () to subtract a value from an array

I want to use pandas apply () instead of iterating over each row of the data frame, which, as far as I know, is a more efficient procedure.

What I want to do is simply:

temp_arr = [0,1,2,3]
# I know this is not a dataframe, just want to show quickly how it looks like.
temp_df is a 4x4 dataframe, simply: [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
For each row in my temp_df, minus the corresponding number in the temp_arr. 

So, for example, the first line in my data frame is [1,1,1,1], and I want to minus the first element in my temp_arr (which is 0) of them, so the output should be [1, 1,1,1 ]. The second line is [2,2,2,2], and I want to minus the second element in temp_arr (which is 1) of them, so the output should also be [1,1,1,1].

If I subtract a constant number, I know that I can easily do this with:

temp_df.apply(lambda x: x-1)

But the tricky thing here is that I need to go through my temp_arr to get a subtracted number. Anyway, can I do this with apply ()?

+4
2

a dataframe df

a = np.arange(4)
df = pd.DataFrame(np.repeat([1, 2, 3, 4], 4).reshape(4, -1))

print(a)

[0 1 2 3]

print(df)

   0  1  2  3
0  1  1  1  1
1  2  2  2  2
2  3  3  3  3
3  4  4  4  4

pd.DataFrame.sub axis=0
axis=0

print(df.sub(a, axis=0))

   0  1  2  3
0  1  1  1  1
1  1  1  1  1
2  1  1  1  1
3  1  1  1  1


numpy

 print(df.values - a[:, None])

[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

dataframe

d1 = pd.DataFrame(df.values - a[:, None], df.index, df.columns)
print(d1)

   0  1  2  3
0  1  1  1  1
1  1  1  1  1
2  1  1  1  1
3  1  1  1  1
+4

:

import numpy as np
import pandas as pd
df = pd.DataFrame(data = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])    
a = pd.DataFrame({'a': np.arange(4), 'b': np.arange(1, 5)})
print df.apply(lambda x: x - a.ix[x.index, 'a'], axis = 1)
print df.apply(lambda x: x - a.ix[x.index, 'b'], axis = 1)

:

import numpy as np
import pandas as pd
term_df = pd.DataFrame(data = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])    
temp_arr = np.arange(4)
print temp_df.apply(lambda x: x - temp_arr[x.index], axis = 1)
0

Source: https://habr.com/ru/post/1665256/


All Articles