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

How to reset a pandas dataframe data types to default or auto detect?
How to add a column to all DataFrames inside a Panel (don't mind using a for loop)
What is the functionality of the filling method when reindexing?
Pandas, multiindex, dates, HDFstore and frame_tables
Binary operation broadcasting across multiindex
Insert or replace like operation for Pandas HDFStore (table)
Parsing excel files with Pandas
grouping by column and then doing a boxplot by the index in pandas
How to create a lagged data structure using pandas dataframe
pandas dataframe interpolate
pandas dataframe interpolating missing days
procedurally convert interval data to cross sectional data
Pandas Resample Strange Zero Tolerance Behavior
VLOOKUP equivalent function to look up value in pandas DataFrame
pandas dataframe shift dates
ipython dataframe plotting setting color parameter?

Categories

HOME
openlayers
variables
clips
hive
oracle11g
heroku
google-play
iterator
react-redux
amazon-ecs
v8
gps
angular-ui-bootstrap
directx
autotools
vault
offline
collectd
remote-access
clojurescript
propel
swingx
nas
visjs
xlsxwriter
one-hot-encoding
intentfilter
functional-testing
restful-authentication
cas
fgetcsv
filezilla
applozic
minitab
xacml
jaxb2
windows-server-2000
windowbuilder
skia
titanium-mobile
contact-form
catch-all
stacked
adobe-premiere
space-complexity
c11
reportingservices-2005
node-sass
thin
smartcontracts
termination
knockout-components
python-cryptography
google-cdn
setuptools
rotativa
login-control
asp.net-4.5
angstrom-linux
moveit
ubuntu-10.04
suffix-tree
livequery
goose
t4mvc
ruby-2.2
nsight
clipperlib
c++03
offloading
nsbutton
internet-connection
document-classification
phpthumb
yorick
xsockets.net
opcache
qt-faststart
ember-app-kit
eventlistener
dmoz
quickdialog
external-accessory
disclosure
celltable
mysql-error-1005
fluent-interface
user-friendly
3gp

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