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

Weighted average for every possible range [closed]
Pandas multi index dataframe to nested dictionarry
No module named zipline (Python 64 bit and Windows 64 bit)
Mapping to similar times in Pandas
Pandas Unmelt dataset
multicollinearity for one-hot encoding
How to work with 'NA' in pandas?
Pandas DataFrame.assign arguments
zipline error KeyError: <type 'zipline.assets._assets.Equity'>
How can i split 'Date' column into 'Date' and 'Time' column? [closed]
Pandas customized group aggregation
What is the significance of t-stats value while applying ttest_ind on two pandas series?
Align order of multiindex dataframe with the order of another multiindex df, pandas
From Object To Date in Pandas [duplicate]
How do I configure PyCharm to Pandas
cannot convert nan to int (but there are no nans)

Categories

HOME
pandas
bluetooth
proxy
reflection
google-play
dot
cplex
xmpp
google-oauth
spring-jdbc
sql-server-2016
windows-10-universal
rascal
clearcase-ucm
tostring
pc
google-static-maps
http-status-code-504
oracle-coherence
autoconf
physics-engine
ehcache
web-sql
pass-by-reference
kudan
sqlcipher
qhull
facet
fopen
typo3-6.2.x
r-raster
fabric8
phonegap
pim
windows-dev-center
cloud-code
overriding
x11-forwarding
twitch
theano.scan
sqlite2
trim
awt
email-templates
android-tabhost
nomethoderror
unixodbc
convertapi
smartcontracts
azure-application-gateway
jquery-validate
typescript1.8
dotnetzip
togetherjs
magma
akka-cluster
angular-strap
url-masking
adxstudio-portals
thrust
qpid
php-internals
jfugue
ios8-today-widget
spidermonkey
coveralls
sysinternals
fluid-dynamics
oberon
dia
tween
gwidgets
pick
xc16
project-planning
starcluster
expected-exception
poller
jboss-weld
concurrent-collections
typo3-neos
prettify
gnu-smalltalk
specification-pattern
path-separator
ticoredatasync
removeclass
php-parser
vdsp
subgurim-maps
genshi
xmlspy
gamma
forums
ext3
nintendo-ds
ti-dsp

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