[英]How to select DataFrame columns for plotting in Pandas?
I have a python DataFrame from Pandas 我有一个来自Pandas的python DataFrame
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. 每次构造此DataFrame时,snapDate之后的列数和名称都不同。
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. 我将需要通过选择
snapDate
作为索引列来构造一个新的DataFrame,但是应该动态选择其余的列以绘制任何想法。 How can I achieve this? 我该如何实现?
for the df
above, I should always select the snapDate
column and all the columns after the snapDate
column 对于上面的
df
,我应该始终选择snapDate
列以及snapDate
列之后的snapDate
列
The number and name of the columns after the snapDate column will vary. snapDate列之后的列数和名称将有所不同。
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. 我想通过始终选择SnapDate列和其余列(根据DataFrame可能是2或3或4等)从DataFrame创建图。
You can use get_loc
to get the index position of the column of interest and then use this to index your df: 您可以使用
get_loc
获取感兴趣列的索引位置,然后使用它来索引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: []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.