gpt4 book ai didi

python - 根据属性分割数据框

转载 作者:行者123 更新时间:2023-12-01 00:19:03 25 4
gpt4 key购买 nike

我有一个关于分割数据框的问题。我希望根据给定的属性对数据进行分组。

例如:我有一个属性 x这是 <0.5 or> 0.5 .
所以我想要聚类给我两个组,

  1. 具有 x <0.5 的数据之一
  2. 另一组,其中 x> 0.5

有解决办法吗?谢谢

最佳答案

这个问题是关于基本编程技术的。

import numpy as np

x = np.random.uniform(low=0, high=1, size=50)
print('Original Values')
print(x)

groupA = []
groupB = []

for xitem in x:
if xitem <= 0.5:
groupA.append(xitem)
else:
groupB.append(xitem)

print('group A')
print(groupA)
print('group B')
print(groupB)

给你:

Original Values
[0.32589696 0.05144454 0.3185115 0.44712084 0.41648028 0.83977423
0.54061874 0.05583937 0.93067456 0.41763108 0.70905716 0.94582328
0.9932486 0.06834 0.54789265 0.68866598 0.47188399 0.62864056
0.04820358 0.21935119 0.5146725 0.40023542 0.97429373 0.24902844
0.39409913 0.96247417 0.57696844 0.67546141 0.79400894 0.44830457
0.29500037 0.77739617 0.53539312 0.75193549 0.22002909 0.19796147
0.1739707 0.40734209 0.10614055 0.43634519 0.24865746 0.43886737
0.54361849 0.21692039 0.33623253 0.01805197 0.71022451 0.52776778
0.16213003 0.78893891]
group A
[0.325896959384964, 0.0514445444953352, 0.3185115042135239, 0.447120840309126, 0.41648028298259887, 0.05583936914128218, 0.41763107913224073, 0.06833999667958424, 0.47188398978792234, 0.04820357539733733, 0.21935118588188507, 0.4002354236237443, 0.24902843579302625, 0.3940991273818705, 0.4483045722482677, 0.2950003683766097, 0.22002909264547388, 0.19796146580392382, 0.17397069583620484, 0.40734208646363645, 0.10614054933682493, 0.43634519025046536, 0.2486574594683204, 0.4388673670602017, 0.2169203865405519, 0.3362325296591817, 0.01805196793659747, 0.16213002704108503]
group B
[0.8397742323096252, 0.540618735072025, 0.9306745584875667, 0.7090571559979788, 0.9458232841023849, 0.9932485951266484, 0.5478926512185291, 0.6886659807258041, 0.6286405561999165, 0.5146724961600602, 0.9742937336700853, 0.962474172144148, 0.5769684433064299, 0.6754614108084783, 0.7940089394834825, 0.7773961745651656, 0.5353931179563641, 0.7519354929404076, 0.5436184941517485, 0.7102245133044452, 0.5277677794818838, 0.7889389082699289]

最后,A 组 包含所有小于或等于0.5 的值,B 组 包含所有其他值。这就是你要问的吗?



发表评论后进行编辑正如PV8在答案中也建议的那样:

import plotly.express as px
import pandas as pd
import numpy as np

df1 = pd.DataFrame(columns=['x', 'y'])
df1['x'] = np.random.uniform(low=0, high=1, size=50)
df1['y'] = np.random.uniform(low=0, high=1, size=50)

df1['group'] = np.where(df1['x'] < 0.5, "A", "B")

fig1 = px.scatter(df1,x="x", y="y", color="group")
fig1.update_layout(height=600, width=800, title_text="Subplots")
fig1.show()

这为您提供了两个聚类并在散点图中可视化的组。 enter image description here

关于python - 根据属性分割数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com