pandas


Apply an element-wise function on a pandas dataframe with index and column values as inputs


I often have this need, and I can't seem to find the way to do it efficiently.
Let's say I have a pandas DataFrame object and I want the value of each element (i,j) to be equal to f(index[i], columns[j]).
Using applymap, value of index and column for each element is lost.
What is the best way to do it?
It depends on what you are trying to do specifically.
clever hack
using pd.Panel.apply
it works because it will iterate over each series along the major and minor axes. It's name will be the tuple we need.
df = pd.DataFrame(index=range(5), columns=range(5))
def f1(x):
n = x.name
return n[0] + n[1] ** 2
pd.Panel(dict(A=df)).apply(f1, 0)
0 1 2 3 4
0 0 1 4 9 16
1 1 2 5 10 17
2 2 3 6 11 18
3 3 4 7 12 19
4 4 5 8 13 20
example 1
Here is one such use case and one possible solution for that use case
df = pd.DataFrame(index=range(5), columns=range(5))
f = lambda x: x[0] + x[1]
s = df.stack(dropna=False)
s.loc[:] = s.index.map(f)
s.unstack()
0 1 2 3 4
0 0 1 2 3 4
1 1 2 3 4 5
2 2 3 4 5 6
3 3 4 5 6 7
4 4 5 6 7 8
or this will do the same thing
df.stack(dropna=False).to_frame().apply(lambda x: f(x.name), 1).unstack()
example 2
df = pd.DataFrame(index=list('abcd'), columns=list('xyz'))
v = df.values
c = df.columns.values
i = df.index.values
pd.DataFrame(
(np.tile(i, len(c)) + c.repeat(len(i))).reshape(v.shape),
i, c
)
x y z
a ax bx cx
b dx ay by
c cy dy az
d bz cz dz

Related Links

How to drop columns from dataframe having all values equal to False(Boolean)?
Pandas: Create a new column with random values based on conditional
Log values by SFrame column
Encode error with df.to_clipboard()
ipython - pandasql.sqldf doesn't return an error
Efficient way to add to a series without duplicates
How to avoid temporary variables when creating new column via groupby.apply
Get value of a Pandas GroupBy Object
Trouble importing Pandas
pandas.io.ga not working for me
adding two series with missing data
Merging/combining two dataframes with different frequency time series indexes in Pandas?
Show DataFrame as table in iPython Notebook
Pandas. Groupby multiple columns, then attach a calculated column to an existing dataframe
pandas dataframe transformation partial sums
Pycharm - Package installation on Windows

Categories

HOME
hook
keras
azure-data-factory
react-router
youtube-dl
rsync
graphql
frameworks
jxls
survey
fingerprint
acquia
flyway4
constraint-programming
midi
kentor-authservices
commonmark
phaser
quickfix
after-effects
propel
ab-testing
jqwidget
conemu
accessor
bootstrap-tour
mmap
cultureinfo
autosys
blazemeter
bytecode-manipulation
vxworks
libraries
reportbuilder
sqlite2
http-live-streaming
password-encryption
s
qcombobox
convertapi
galleria
fault
youcompleteme
atomicity
pubmed
return-value
libusb-win32
sybase-asa
imanage
hittest
titanium-android
query-performance
angular-strap
static-ip-address
elements
transmitfile
nsviewcontroller
sigabrt
master-slave
captivenetwork
pagerank
energy
ios8-today-widget
spim
xna-4.0
python-3.2
wordml
wireshark-dissector
splash
cisco-ios
jsapi
apache-commons-net
phalanger
html-helper
rdl
terminal-services
mqx
android-hardware
clipper
getmessage
netbeans-6.9
amazon-appstore
semantic-diff
custom-backend
gnustep
actionview
subviews
zend-translate
dmx512
mozilla-prism
database-management
w3wp
lzh
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App