1

I have a python DataFrame from Pandas

waitEvent   instance    snapDate    gc cr block 3-way   gc current block 3-way  log file sync

Every time I construct this DataFrame, the number and name of columns after the snapDate are different.

I would need to construct a new DataFrame by selecting snapDate as the indexcolumn but the remaining columns should be dynamically selected for plotting any ideas. How can I achieve this?

for the df above, I should always select the snapDate column and all the columns after the snapDate column

The number and name of the columns after the snapDate column will vary.

My objective is to do

df[snapDate,col1,col2,col3].plot()
df[snapDate,col7,col8].plot()
..

I want to create a plot from the DataFrame by always picking the SnapDate column and the remaining columns which depending on the DataFrame could be 2 or 3 or 4 etc.

3
  • are you sure you question is correclty formatted? I do not see the data. You want basically to create a plot function, correct? Commented Jul 15, 2015 at 11:25
  • Will snapDate always be the 3rd column? In that case, you can select the columns after it with : df.iloc[:,2:] Commented Jul 15, 2015 at 11:31
  • Hi Colonel data doesn't matter for this example i guess.I can post some sample data if required.Stellasia i want to include snapDate column and all the columns after snapDate Commented Jul 15, 2015 at 12:05

1 Answer 1

1

You can use get_loc to get the index position of the column of interest and then use this to index your df:

In [374]:
cols = ['waitEvent','instance','snapDate','gc cr block 3-way','gc current block 3-way','log file sync']
df = pd.DataFrame(columns = cols)
df

Out[374]:
Empty DataFrame
Columns: [waitEvent, instance, snapDate, gc cr block 3-way, gc current block 3-way, log file sync]
Index: []

In [378]:    
snapDateIdx = df.columns.get_loc('snapDate')
snapDateIdx

Out[378]:
2

In [379]:
df.ix[:,snapDateIdx:]

Out[379]:
Empty DataFrame
Columns: [snapDate, gc cr block 3-way, gc current block 3-way, log file sync]
Index: []
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.