pandas


Filtering columns in dataframe that begin with a specific string


I have the following df, and I would like to apply a filter over the column names and simply remain those that begin with a certain string:
This is my current df:
ruta2:
Current SAN Prev.1m SAN Prev.2m SAN Prev.3m SAN Current TRE \
A 5 6 7 6 3
B 6 5 7 6 6
C 12 11 11 11 8
Basically what I would like is to filter the dataframe and remain the columns that begin with Current.
Then the desired output would be:
ruta2:
Current SAN Current TRE
A 5 3
B 6 6
C 12 8
In order to do this I tried this filter but outputs a value error :
ruta2=ruta2[~(ruta2.columns.str[:4].str.startswith('Prev'))]
It seems you only need:
ruta2=ruta2.loc[:, ~(ruta2.columns.str[:4].str.startswith('Prev'))]
#same as
#ruta2=ruta2.loc[:, ~ruta2.columns.str.startswith('Prev')]
print (ruta2)
Current SAN Current TRE
A 5 3
B 6 6
C 12 8
Or:
cols = ruta2.columns[ ~(ruta2.columns.str[:4].str.startswith('Prev'))]
ruta2=ruta2[cols]
print (ruta2)
Current SAN Current TRE
A 5 3
B 6 6
C 12 8
But if need only Current columns use filter (^ means start of string in regex):
ruta2=ruta2.filter(regex='^Current')
print (ruta2)
Current SAN Current TRE
A 5 3
B 6 6
C 12 8
#filter the columns names starting with 'Current'
ruta2[[e for e in ruta2.columns if e.startswith('Current')]]
Out[383]:
Current SAN Current TRE
A 5 3
B 6 6
C 12 8
Or you can use a mask array to filter columns:
ruta2.loc[:,ruta2.columns.str.startswith('Current')]
Out[385]:
Current SAN Current TRE
A 5 3
B 6 6
C 12 8

Related Links

Rearrange rows of pandas dataframe based on list and keeping the order
pandas: merge/concat.join two dfs
Scikit learn for ranking features using RFE including scores
adding a column to Pandas dataframe based on adjacent values of existing column
Pandas merging rows with the same value and same index
I update to pandas 0.18 with conda update command. But my jupyter notebook cannot import that version and keep on using 0.15
Pandas timeseries resampling and interpolating together
assign values to a subset of rows based on a count criteria from columns in pandas
numpy array format for cross_validation
Plotting in pandas dataframe using matplotlib
Getting csv header in pandas from arbitrary row
How do I give the index column created by a functional groupby a name?
Pandas: substitute NA's with shadow values obtained from contemporary non missing values in other column
How to sort from greatest to smallest of groupby data in Pandas Python
pandas: stop pandas from auto displaying 0.40 as 0.4
How can I manage units in pandas data?

Categories

HOME
atom-editor
omnet++
iot
vmware
android-4.4-kitkat
xmpp
maven-3
autotools
primary-key
jplayer
size
visual-studio-2005
django-simple-history
pc
windows-7-x64
pugjs
propel
solaris-10
text-rendering
autocad-plugin
microsoft-r
wijmo
lcd
autosys
vlsi
sparse-matrix
facebook-access-token
brunch
filezilla
applozic
strncpy
neo4j-spatial
ping
saas
dbclient
jspm
gitignore
vision
kendo-datasource
quote
sql-server-2012-express
tinymce-4
socialengine
frame
yadcf
xmlreader
mozilla
android-nestedscrollview
geopositioning
atl
bower-install
adobe-premiere
web-mining
sqlite2
opshub
ws-security
google-closure
angular2-meteor
avconv
xml-documentation
typescript1.8
pebble-watch
recursive-datastructures
np-complete
flow-control
linode
iso8601
jspdf-autotable
time-and-attendance
url-pattern
httplistener
login-control
lua-5.1
superstack
dlna
pagerank
rtbkit
separator
iis-arr
embedded-code
ibaction
sankey-diagram
createprocessasuser
census
c3
xc16
file-copying
internet-connection
neolane
zend-route
flask-cors
uitouch
phpthumb
braille
concurrent-collections
meteor-velocity
viadeo
intentservice
html5-notifications
eventual-consistency
django-nonrel
interface-orientation
ember-app-kit
eventlistener
distutils
errai
nsmanagedobject
spyware
django-tagging
table-footer
lang
digest-authentication
compiler-specific
privilege
yagni
procedural-music
ubuntu-9.04

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile