![](/img/trans.png)
[英]How to plot a heatmap using plt.imshow() from pandas dataset X and Y co-ordinates?
[英]Using slicing and indexing of PANDAS dataframe to pass as x y co-ordinates to Matplotlib
嗨,我在提取PANDAS数据框中的两列的部分时遇到了麻烦,每一列都变成了matplotlib图的x和y坐标。
我有一些硬件可以生成这样的数据:
Some text here
start=0.2300 end=0.6800
Col0 Col1 Col2 Col3 Col4 Col5
0.0000 1.262257 -2.358180 0.659226 -0.148103 -0.396258
0.1100 0.036566 -0.119857 1.305843 0.445102 0.586046
0.2300 1.090691 -0.957441 -1.507930 -0.009555 0.153534
0.3700 -0.582869 0.719073 1.092839 0.933759 0.077195
0.4200 -1.153562 0.950388 0.207947 0.678560 0.299933
0.5400 0.164422 -0.927772 0.642784 1.522672 0.483578
0.6800 1.178238 -2.597361 1.039083 -2.056909 0.117656
0.7500 0.144455 0.758041 -0.322278 0.152447 0.611217
我想做的是在开始时间和结束时间之间提取Col0作为x坐标传递,并提取相同范围的Col5作为y坐标传递
我进行了正则表达式搜索,以隔离存储为“开始”和“结束”的第3行中的开始和结束代码。 我还可以获得相关的开始和结束部分的索引。
loop_start = df[df['Col0'] == start].index.item
loop_end = df[df['Col0] == end].index.item
当我尝试切片准备传递给matplotlib的数据帧时,问题就开始了。 当我尝试
y = df.iloc[loop_start:loop_end, 5].values
x = df.iloc[loop_start:loop_end, 0].values
这返回
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [<bound method Int64Index.item of Int64Index([0], dtype='int64')>] of <class 'method'>
由于它们的“开始”和“结束”值最初是浮点型的,所以我将它们转换为int,并且我认为这可能使==语句无法正常工作。
在这个阶段的任何帮助都是很好的。
如果将ColO设置为索引,则可以轻松获取以下子集:
start = 0.23
end = 0.68
df = df.set_index('Col0')
df_subset = df[start:end]
您应该只使用.loc
来子集您的DataFrame
start = 0.23
end = 0.68
x,y = df.loc[df.Col0.between(start, end), ['Col0', 'Col5']].values.T
x
#array([0.23, 0.37, 0.42, 0.54, 0.68])
y
#array([0.153534, 0.077195, 0.299933, 0.483578, 0.117656])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.