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

Pandas data frames and matplotlib.pyplot
Pandas.to_csv thousand separator
Annotating a graph with certain values of another series (Index is datetime)
Pandas rolling sum on string column
pandas apply() with and without lambda
Pandas read_html to retrieve Table
pandas: reshape dataframe for stacked bar plot
Change values in a column from a list
Pandas: How to Return Max Value in Multiindex
cx freeze module not correctly installed
Counting occurences from a dict and pandas
Calculating mean of a specific column by specific rows
Value counts per period, taking prior values into account
mapping values from another pandas df
Tensorflow: Cannot allocate buffer larger than kint32max for StringOutputStream
using assign and lambda to combine year and month columns into 1 date column

Categories

HOME
date
deployment
magnific-popup
push-notification
onedrive
platform-builder
kalman-filter
gis
elm
graphql
rubygems
jxls
umd
baqend
enterprise-library-5
qore
node-notifier
alignment
fortumo
windows-server-2012
systemc
angular-ui
clickonce
scriptcs
bootstrap-tour
captiveportal
autosys
bootstrap-duallistbox
user-interaction
windows-error-reporting
maquette
facebook-apps
large-file-upload
libssl
librato
media-queries
noraui
azure-sql-database
bower-install
host
sharefile
bootstrapper
nomethoderror
password-encryption
reactive-cocoa-5
sencha-touch-2.3
skeleton-css-boilerplate
outlook-2013
zip4j
domain-model
dropbox-php
powercli
carrot
windows-iot-core-10
nbconvert
root-framework
watchconnectivity
cubes
angular-strap
color-picker
google-feed-api
asp.net-4.5
xml-attribute
moveit
browser-link
hsv
kendonumerictextbox
rtbkit
intel-fortran
teamcity-8.0
lemon
iis-arr
kcachegrind
wyam
responsive-images
nstableviewcell
applescript-objc
client-side-templating
xcode-6.2
preferences
flask-cors
eol
sgen
phpthumb
poller
concurrent-collections
html5-notifications
prettify
oam
quantlib-swig
batterylevel
django-nonrel
pushbackinputstream
simba
netbeans-6.9
jquery-ui-layout
adk
pydot
spyware
vdsp
xmlspy
table-footer
recent-documents
sql-server-profiler
mozilla-prism
user-friendly
multi-tier
project-hosting
.net-1.0

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