python


Replace integers with labels across different columns - pandas


I have a panda data frame with several column of integers, as well as a corresponding dictionary of {column : {integer:string_label}}.
I am trying to create a dataframe in which the integers have been replaced by their labels. The closest I got is below, but the output is somewhat unexpected.
Code
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,3,3,8],'b':[8,8,8,8,7]})
dic = {'a':{1:"label1",2:"label2",3:"label3"}, 'b':{8:'label8',7:'label7'}}
converters = {column: lambda x: dic[column][x] if x in dic[column].keys() else np.nan for column in dic.keys()}
new = pd.DataFrame.from_dict({col: series.apply(converters[col])
if col in converters else series
for col, series in df.iteritems()})
print new
#Output:
# a b
# 0 NaN label8
# 1 NaN label8
# 2 NaN label8
# 3 NaN label8
# 4 label8 label7
The problem is that you're using the variable column inside the lambda function, the lambda declaration won't store the value, it will use what the variable holds at the time it is being called ( in series.apply(converters[col])), and it can be anything. In fact, if you run your code some times you will notice that it gives different results.
A possible solution:
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,3,3,8],'b':[8,8,8,8,7]})
dic = {'a':{1:"label1",2:"label2",3:"label3"}, 'b':{8:'label8',7:'label7'}}
new = pd.DataFrame.from_dict({col: series.apply(lambda x: dic[col][x] if x in dic[col].keys() else np.nan)
if col in dic.keys() else series
for col, series in df.iteritems()})
print(new)
import numpy as np
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3, 3, 8], 'b': [8, 8, 8, 8, 7]})
dic = {'a': {1: "label1", 2: "label2", 3: "label3"},
'b': {8: 'label8', 7: 'label7'}}
df = df.replace(dic)
allowed = {k: v.values() for k, v in dic.items()}
for col_name, allowed_col_vals in allowed.items():
# Let's replace not allowed values by NaN
df[col_name][~df[col_name].isin(allowed_col_vals)] = np.nan
The df ends up like this:
a b
0 label1 label8
1 label2 label8
2 label3 label8
3 label3 label8
4 NaN label7

Related Links

How to call a function after adding values to an Option Menu?
using python to verify that a shapefile is a shapefile (fiona, ogr)
Identifying largest connected component in a matrix
VBA shell script set environment variable before execution
Embed matplotlib in PyQt
Odoo - overriding old api method with new api?
Pylint warning - W5304(Missing-Test-Class)
In terms of efficiency/quickly rejecting things, does it matter if your if statements are on the same line?
QComboBox drop-down list adding unnecessary scroll bar
Specify color type for bar graph pylab
prettifying a part of the html doc using beautifulsoup
Django concatenate two querysets for same model
How can I validate input to accept only binary numbers?
Custom route predicates in Pyramid
Why is my query for a structured property failing with BadFilterError?
Simple Audio Units Host to drive an Audio Units Instrument

Categories

HOME
jdbc
ember.js
ssas-2012
system-verilog
warnings
boost-thread
reportportal
hana
rfc
bro
triggers
game-physics
gwtp
cratedb
symfony2-easyadmin
jacoco
netezza
jquery-terminal
google-api-java-client
prestodb
koa
flexboxgrid
esql
countif
npm-install
bootstrap-typeahead
novnc
scalajs-react
hash-collision
codelite
reactiveui
ivy
red-black-tree
helix-3d-toolkit
pdflatex
pycparser
mapguide
nlb
codesys
socketpair
sonarqube-scan
android-preferences
lexical-analysis
exiftool
nashorn
resuming-training
prototypejs
onmouseover
rdw
w3-total-cache
chown
gd
ticker
pagefile
photography
spyne
pdfkit
svn-merge
coremidi
cfeclipse
hspec
pydio
excon
qregexp
contour
uwp-maps
string-parsing
handlebars.java
brackets-shell
magento-1.4
license-key
void
asymptote
jenkins-scriptler
insert-into
uialertview
futuretask
anti-patterns
base32
on-duplicate-key
leadtools-sdk
twitter-rest-api
adodb
orientation-changes
rhel5
cloudpebble
line-numbers
node-inspector
node-imagemagick
io.js
neoload
jstack
db4o
bfd
vertical-rhythm
swrl
justgage
surveyor-gem
lame
mailcore
xslkey
vertical-scrolling
snapjs
domain-calculus
xgettext
mpmovieplayer
objective-c-protocol
userid
hinstance
objectbrowser
tcxgrid
msbuildextensionpack
.app
rijndael
communicationexception
iphone-sdk-3.2
plinq
visual-studio-dbpro

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