Reputation: 3729
My data frame has many columns, but for some, they start with similar words as column names:
>>> df.columns
Index([u'VESSELTYPE', u'VESSELNAME', u'PERIODSTART', u'PERIODEND', u'ESTREDELI', u'HIRE', u'DAYS Mar-14', u'DAYS Q2 2014', u'DAYS Q3 2014', u'DAYS Q4 2014', u'DAYS Q1 2015', u'DAYS Q2 2015', u'DAYS Q3 2015', u'DAYS Q4 2015', u'NUMDAYS'], dtype='object')
>>>
See here, I want to select all with 'DAYS' in front, is there any thing like in SQL, I can use *
to represent everything after 'DAYS'?
Upvotes: 1
Views: 2172
Reputation: 4068
To get only the days columns of your dataframe do:
print df[[x for x in df.columns if "DAY" in x]]
Upvotes: 0
Reputation: 35069
pandas indexes (and dataframes) are numpy arrays, so you can use Numpy's index tricks - in particular, indexing with a vector of bools can is helpful here. To test whether any particular entry starts with "DAYS" is a matter of Python's standard string methods; put this in a list comprehension to get your list of bools:
df.columns[[x.startswith("DAYS") for x in df.columns]]
or you can use the map
method on arrays to avoid the double brackets:
df.columns(df.columns.map(lambda x: x.startswith("DAYS"))
Upvotes: 1
Reputation: 7845
Try to adapt this to your code.
l = ([u'VESSELTYPE', u'VESSELNAME', u'PERIODSTART', u'PERIODEND', u'ESTREDELI', u'HIRE', u'DAYS Mar-14', u'DAYS Q2 2014', u'DAYS Q3 2014', u'DAYS Q4 2014', u'DAYS Q1 2015', u'DAYS Q2 2015', u'DAYS Q3 2015', u'DAYS Q4 2015', u'NUMDAYS'], 'AnotherItem')
days = [s for s in l[0] if s.startswith('DAYS')]
print days
You can use the "startswith()" method available to all strings and make some logic with it.
If you don't like list-comprehensions, here's its equivalent in a for-loop:
days = []
for s in l[0]:
if s.startswith('DAYS'):
days.append(s)
print days
Upvotes: 3
Reputation: 27692
Yes, you can try something like this:
'DAYS' == x[:len('DAYS')]
Where you are comparing your target string to the string first n characters. Being n the length of your target string.
You can use this condition in your selection.
Upvotes: 0