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

bar plot - annotate the bars with some values
How to create pandas dataframe with date as index
pandas map and timedelta with missing values
How to summarise data by percentages in pandas
python pandas groupby plot with sorted date as xtick
python Pandas groupby method
Pandas time series plot - setting custom ticks
Python / Pandas Dataframe change specific value
localize timestamp in pandas
Pandas Merge duplicate index into single index
Pandas : How to get timestamp.day and timestamp.month with padded zero
Resample TimedeltaIndex and normalize to frequency
Memory error when running medium sized merge function ipython notebook jupyter
Aggregate/Remove duplicate rows in DataFrame based on swapped index levels
Change color of legend to match line plot matplotlib pandas
TypeError: pivot_table() got an unexpected keyword argument 'rows'

Categories

HOME
ms-access
winforms
azure-data-factory
ngrx
dictionary
google-docs
cvs
graphql
electronics
fsm
ios-charts
synchronization
amazon-cloudformation
slurm
gitpitch
maude-system
node-pdfkit
messages
caml
worldwind
after-effects
angular-ui
connection-string
google-cloud-speech
firefox-webextensions
lucene.net
autocad-plugin
kryo
csrf-protection
shopware
microsoft-sync-framework
qhull
fish
cas
bootstrap-duallistbox
phpfox
fgetcsv
tooltipster
entitlements
typo3-6.2.x
windowbuilder
directx-10
bosh
noraui
android-nestedscrollview
http-referer
html5-fullscreen
dartium
dynamics-crm-2013
code-search-engine
document.write
temporary-files
unoconv
email-templates
ensembles
pdf-reactor
quadratic-programming
android-mediaprojection
estimote
http-redirect
wso2carbon
xml-documentation
filepicker
dandelion
integrity
setter
diagnostics
directory-structure
spring-cache
dynamics-sl
redundancy
synchronous
deadbolt-2
angular-cache
log4c
ado.net-entity-data-model
adxstudio-portals
thrust
nsfilemanager
pagedlist
actionbardrawertoggle
pickadate
rhino-servicebus
photobucket
energy
ios8-today-widget
embedded-code
apache-commons-fileupload
device-orientation
ng-animate
phalanger
codeigniter-routing
nsbutton
openlaszlo
industrial
prettify
farseer
geos
commoncrypto
ftps
orchardcms-1.7
delphi-6
hamiltonian-cycle
labwindows
viewswitcher
hosts-file
uiviewanimation-curve
custom-backend
data-loss
forums
microsoft-virtualization
zend-decorators
zune
post-build
ugc

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App