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

Merge two data frames based on common column values in Pandas
Combine two columns of numbers in dataframe into single column using pandas/python
Python Pandas has low CPU usage and not using all cores
Pandas Set on copy warning when using .loc
Merge very large csv using pandas or awk
seasonal_decompose: operands could not be broadcast together with shapes on a series
How to properly sample from a numpy.random.multivariate_normal (positive-semidefinite covariance matrix issue)
how to do logical operation between dataframe columns?
Console hangs up at the time of plotting
Pandas apply a function at fixed interval
float type column in pandas to convert to tuple/list
Getting an error with Pandas Panel boolean indexing
pandas dataframe subtraction causing nan
Pandas dataframe: truncate string fields
how to add new categorical column in pandas
Finding different Ids with the same value in pandas dataframe

Categories

HOME
date
azure-data-factory
deezer
homebrew
hashmap
session
at-command
rubygems
ebean
enterprise-library-5
webrequest
virtualization
basic
cloudkit
node-pdfkit
moonmail
msp430
progressive-web-apps
ef-migrations
aurigma
database-replication
excel-vba-mac
crystal-reports-2008
autoconf
format-specifiers
rundeck
semantic-analysis
url-scheme
info.plist
nhapi
maquette
elasticsearch-ruby
jspm
titanium-mobile
scorm2004
kendo-ui-grid
framemaker
hybridauth
protovis
avcapturesession
overlap
main
abstract-class
webdriver-manager
squib
powershell-dsc
node-gyp
eclipse-gef
pnotify
celery-task
hls.js
return-value
feeds
r-forge
flow-control
hittest
sonarlint-vs
pycaffe
angular-strap
ado.net-entity-data-model
merge-conflict-resolution
firebaseui
design-by-contract
system.management
photobucket
lemon
sdhc
coveralls
microbenchmark
iiviewdeckcontroller
e10s
apache-commons-net
system.reflection
tablelayout
asp.net-web-api-odata
typeof
balanced-payments
ember-charts
srs
imdbpy
pyhdf
dir
odata4j
ftps
buster.js
padarn
jspinclude
spring-portlet-mvc
window-management
pydot
subgurim-maps
coercion
recent-documents
telerik-scheduler
jquery-ui-droppable
preference
nsviewanimation
sef
photoshop-cs4
mediarss
mdac
geneva-server

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