Gulzar
Gulzar

Reputation: 27946

How to make the same calculation for multiple new columns in pandas?

Assume the following dataframes:

df1:

a        b    
'a'      'aaaaa'
'bb'     'bbbb'
'ccc'    'ccc'
'dddd'   'dd'
'eeeee'  'e'

df2:

c       d    
'aaa'   'a'
'bbb'   'bb'
'ccc'   'ccc'
'ddd'   'dddd'
'eee'   'eeeee'

I want to make the same calculation for all columns, which yields a new column.

Assume the calculation is lambda x,y: len(x)+len(y)

Then the result df would be:

ac     bd    
4      6
5      6
6      6
7      6
8      6

What I care about is avoiding the for loop over all the columns/elements. How can I do the same calculation over all relevant columns column_list1, column_list2?


Performed major edit to give better non trivial example (I hope)

Upvotes: 1

Views: 50

Answers (2)

piRSquared
piRSquared

Reputation: 294258

applymap

c = df1.columns + df2.columns
d1 = df1.set_axis(c, axis=1, inplace=False)
d2 = df2.set_axis(c, axis=1, inplace=False)

d1.applymap(len) + d2.applymap(len)

   ac  bd
0   4   6
1   5   6
2   6   6
3   7   6
4   8   6

stack and str.len

c = df1.columns + df2.columns
d1 = df1.set_axis(c, axis=1, inplace=False)
d2 = df2.set_axis(c, axis=1, inplace=False)

(d1.stack().str.len() + d2.stack().str.len()).unstack()

Upvotes: 3

BENY
BENY

Reputation: 323226

Is this what you need ?

df1[:]=df1.values+df2.values
df1
Out[65]: 
       a     b
0  110.0  11.0
1  220.0  22.0
2  330.0  33.0
3  440.0  44.0
4  550.0  55.0

Just assign the columns df1 back to df2

df2.columns=df1.columns
df1+df2

Update

df2.columns=df1.columns
(df1+df2).apply(lambda x : x.str.len(),1)
Out[81]: 
   a  b
0  4  6
1  5  6
2  6  6
3  7  6
4  8  6

Upvotes: 2

Related Questions