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 dataframe without copy
Pandas: aggregation on multi-level groups
How to select last row of Pandas DataFrame with Multiindex?
Pandas select row of data frame by integer index
Filter on Pandas DataFrame with datetime columns raises error
How to divide the value of pandas columns by the other column
Pandas regression calculating 'nan' for some standard errors?
How to make first level of MultiIndex as the columns?
Use Pandas for best fit line on time based data
Reading variable column and row structure to Pandas by column amount
Why can I not change the values in a subset of cells from a DataFrame column?
adding a new column with values from the existing ones
How to suppress Pandas Future warning ?
How to delete rows with duplicate values in succeeding rows
How can I check if the values in a series are contained in any of the intervals defined the rows of a DataFrame?
MultiIndex and read_table with separate indexes and data

Categories

HOME
ms-access
ibm-bluemix
activiti
hp-exstream
cplex
youtube-dl
jsp-tags
jira
yahoo-oauth
pivotal-cloud-foundry
podio
slick-slider
dacpac
designer
sms-gateway
visjs
clickonce
kvc
mustache.php
restful-authentication
rundeck
emgucv
fish
rst2pdf
maxmind
microsoft-chart-controls
binary-data
jspresso
typed.js
stringtemplate
azure-sql-database
form-data
accelerate-framework
nssegmentedcontrol
android-ble
overlap
jquery-bootgrid
abstract-class
reportbuilder
lumberjack
core-plot
password-encryption
scorm
react-scripts
return-value
veracode
android-cursor
windows-mobile-6.5
spring-cache
django-scheduler
quartz-composer
background-service
static-ip-address
phishing
fody
superstack
wso2cloud
sdhc
python-3.2
iis-arr
streambase
lustre
qpainter
gadt
titanium-modules
preferences
googlemock
hyprlinkr
xsockets.net
ember-app-kit
limejs
multipage
ocx
assembly-loading
transactionscope
nsdatecomponents
hirefire
cinema-4d
w3wp.exe
paster
gallio
sproutcore-2
filtered-index
exchange-server-2003
adrotator
privilege
yagni
port-number
msdev
ugc
commodore
uiq3

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