Pandas: How does df.apply(lambda work to create a result

Veek M veekm at foo.com
Wed May 26 10:45:42 EDT 2021


t = pd.DataFrame([[4,9],]*3, columns=['a', 'b'])
   a  b
0  4  9
1  4  9
2  4  9

t.apply(lambda x: [x]) gives
a    [[1, 2, 2]]
b    [[1, 2, 2]]
How?? When you 't' within console the entire data frame is dumped but how are 
the individual elements passed into .apply()? I can't do lambda x,y: [x,y] 
because only 1 arg is passed so how does [4] generate [[ ]]

Also - this:
 t.apply(lambda x: [x], axis=1)
0    [[139835521287488, 139835521287488]]
1    [[139835521287488, 139835521287488]]
2    [[139835521287488, 139835521287488]]
vey weird - what just happened??

In addition, how do I filter data eg:  t[x] = t[x].apply(lambda x: x*72.72) I'd 
like to remove numeric -1 contained in the column output of t[x]. 'filter' only 
works with labels of indices, i can't do t[ t[x] != -1 ] because that will then 
generate all the rows and I have no idea how that will be translate to 
within a .apply(lambda x... (hence my Q on what's going on internally)

Could someone clarify please.

(could someone also tell me briefly the best way to use NNTP and filter
out the SPAM - 'pan' and 'tin' don't work anymore afaik
[eternal-september]  and I'm using slrn currently - the SLang regex is 
weird within the kill file - couldn't get it to work - wound up killing
everything when I did
Subject: [A-Z][A-Z][A-Z]+
)


More information about the Python-list mailing list