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: 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
Apply an element-wise function on a pandas dataframe with index and column values as inputs
How to pass dataset directory in google datalab
After rename column get keyerror
pandas groupby and mean aggregation on more columns
str.replace function creating NaN data
pandas element wise conditional return index
pandas series or tidy dataframe: index level values to dataframe columns
pyspark dataframe count distinct value row by row considering history

Categories

HOME
testing
google-chrome-extension
multithreading
reflection
mediawiki
stock
octobercms
webstorm
frameworks
packages
v8
survey
convolution
medical
dacpac
quicklook
php-7.1
quickfix
quartz-scheduler
clearcase-ucm
serilog
beyondcompare
nhibernate-envers
object-detection
java-3d
devops
jndi
wpfdatagrid
uninstall
entitlements
social-media
srcset
instant-messaging
socialengine
jspresso
http-get
noraui
geopositioning
vxworks
qwerty
event-driven
powershell-dsc
reactive-cocoa-5
businessworks
babel-core
flickr-api
fault
winscp-net
hls.js
dotnetzip
rdfs
google-perftools
spring-android
angular-cache
azure-sdk
slicknav
intrusion-detection
paypal-express
bstr
qpid
essence
qgraphicsview
oauth2client
intel-fortran
google-places
tarjans-algorithm
angular-leaflet-directive
0xdbe
service-accounts
mmc
angular-local-storage
p2
offloading
phalanger
applescript-objc
onactivityresult
google-reader
android-radiobutton
heisenbug
uitouch
contenttype
jquery-layout
doskey
comexception
prettify
dexterity
sitemesh
spring-io
google-cloud-save
soundtouch
dataadapter
cassini-dev
distutils
dsn
appconkit
nsmanagedobject
bigcouch
cinema-4d
appender
visitor-statistic
sudzc
asp.net-mvc-areas
procedural-music

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