在python中使用timeframe将数据分组

2024-07-05 09:34:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为df的数据帧,它是这样的,但实际上是[9147行x 3列]

indexID  RngUni[m]  PowUni[dB]
157203   1.292283      132
157201   1.271878      132
157016   1.285481      134
157404   1.305886      136
157500   1.353496      136
157524   1.251474      136
157227   1.292283      132
157543   1.339893      136
157903   1.353496      138
156928   1.299084      134
157373   1.299084      136
156937   1.414709      134
157461   1.353496      136
157718   1.360297      138
157815   1.326290      138
157806   1.271878      134
156899   1.360298      134
157486   1.414709      138
157628   1.271878      136
157405   1.299084      134
157244   1.299084      134
157522   1.258275      136
157515   1.367099      138
157086   1.305886      136
157602   1.251474      134
157131   1.265077      132
157170   1.380702      138
156904   1.360297      134
157209   1.401106      138
157018   1.265077      134

我要做的是选取表中数据的某些值。你知道吗

df.plot(x = 'RngUni[m]', y = 'PowUni[dB]', kind = 'scatter')给出:enter image description here

假设主组是大多数数据点聚集的区域,我需要做的是选取主组中80%的点和主组外20%的点。你知道吗

我需要作为列表输出的所有点的索引。我该怎么做?你知道吗

需要集群的一个例子。我想做的是选择80%的圆内点和20%的圆外点。 enter image description here


Tags: 数据区域df列表dbplot集群例子
1条回答
网友
1楼 · 发布于 2024-07-05 09:34:14

以下是我将如何完成这项任务:

from io import StringIO
import pandas as pd
from sklearn.cluster import KMeans

s = '''indexID  RngUni[m]  PowUni[dB]
157203   1.292283      132
157201   1.271878      132
157016   1.285481      134
157404   1.305886      136
157500   1.353496      136
157524   1.251474      136
157227   1.292283      132
157543   1.339893      136
157903   1.353496      138
156928   1.299084      134
157373   1.299084      136
156937   1.414709      134
157461   1.353496      136
157718   1.360297      138
157815   1.326290      138
157806   1.271878      134
156899   1.360298      134
157486   1.414709      138
157628   1.271878      136
157405   1.299084      134
157244   1.299084      134
157522   1.258275      136
157515   1.367099      138
157086   1.305886      136
157602   1.251474      134
157131   1.265077      132
157170   1.380702      138
156904   1.360297      134
157209   1.401106      138
157018   1.265077      134'''

ss = StringIO(s)
df = pd.read_csv(ss, sep=r"\s+")
kmeans = KMeans(n_clusters=2, random_state=0).fit(df.values[:,[1,2]])
df['labels']=kmeans.labels_
df['labels']=kmeans.labels_
df.labels.apply(lambda x: 'red' if x==1 else 'blue')

plt.scatter(x=df['RngUni[m]'], y=df['PowUni[dB]'], c=df['labels'])

输出: enter image description here

只需更改聚类算法并使用参数即可得到所需的聚类和颜色。你知道吗

希望有帮助。你知道吗

相关问题 更多 >