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

Is there a way to select a column that is an index in pandas?
MonthBegin current month with anchor dates included
pandas: map color argument by multidict
Convert categorical variables from String to int representation
using matplotlib colormap with pandas dataframe.plot function
Why head() function on a panda's dataframe displays nothing?
Convert pandas to dictionary defining the columns used fo the key values
Understanding pandas interpolation function
How can I select the indexes where my dataframe has more than two entries?
Pandas HDFS Unicode Issue
Tobin's Annualized Standard Deviation in Pandas
How do you read a geojason url into a geopandas dataframe or pandas dataframe?
Add column index to existing pandas dataframe
IPython : groupby column to find processing time
How to get the value in front of a specified value from Series
Extract specific columns from a given webpage

Categories

HOME
webpack
netbeans
openstack
mediawiki
relay
kalman-filter
rsync
programming-languages
tizen-web-app
leon
youtube-api-v3
electronics
disassembler
multiple-records
add
basic
u-sql
etl
ibm-odm
footer
nhibernate-envers
google-pagespeed
jquery-ajaxq
orleans
arabic
chromium-embedded
jndi
trading
django-storage
chromebook
intel-pin
gsmcomm
nouislider
jspresso
vxworks
hp-ux
powershell-dsc
http-live-streaming
turbogears
sencha-touch-2.3
qwt
react-scripts
clean-architecture
hls.js
kbuild
lync-client-sdk
crosswalk-runtime
android-textview
intrusion-detection
qpid
nsviewcontroller
essence
qgraphicsview
uid
spim
r-tree
reactive-banana
cartesian-product
collapse
spidermonkey
iis-arr
varargs
myo
sailfish-os
web-controls
c++03
angularjs-ng-click
applescript-objc
websocket4net
castle
android-radiobutton
wp-query
valuechangelistener
balanced-payments
imdbpy
html5-notifications
ms-project-server-2010
sitemesh
soundtouch
getmessage
ticoredatasync
chrono
netbeans-6.9
spring-portlet-mvc
subgurim-maps
blackberry-playbook
infobox
xdomainrequest
oncheckedchanged
mongomapper
lang
coda-slider
xsdobjectgen
castle-monorail
project-hosting
post-build
rtml

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile