python - 如何向 Pandas 中的每一行广播统一的随机数?
问题描述
我在这个外观中有一个数据框:
0:00 0:30 1:00 1:30 2:00 2:30 3:00 3:30 4:00 4:30
0 -1.149 -2.105 -2.036 -1.854 -1.807 -0.728 -1.181 -1.801 -0.665 -0.076
1 -1.136 -1.559 -1.332 -1.184 -0.097 -1.215 -0.386 -0.703 -1.176 -0.557
2 -1.147 -1.336 -1.326 -1.568 -1.344 -1.305 -1.398 -1.175 -1.561 -0.439
3 -1.323 -1.337 -1.326 -1.556 -1.348 -1.656 -1.046 -2.715 -1.810 -0.815
4 -1.175 -1.689 -1.505 -1.232 -0.115 -1.259 -0.406 -0.736 -1.652 -0.085
5 -1.243 -1.306 -1.355 -1.539 -1.353 -0.443 -0.853 -1.275 -0.939 -0.679
6 -1.277 -1.728 -1.453 -1.351 -1.271 -0.090 -1.503 -0.984 -0.679 -0.245
7 -1.281 -1.402 -1.491 -1.392 -1.485 -0.167 -1.414 -0.800 -0.635 -1.354
8 -2.079 -1.767 -1.453 -1.548 -1.446 -0.699 -1.235 -1.678 -0.698 -1.037
9 -1.190 -2.238 -1.988 -1.866 -0.932 -1.671 -0.858 -1.275 -2.388 -0.198
我想添加几个新行。新行应该从现有行中随机选择(选择整行),并稍微改变一下。我想对每个新行应用统一的随机数。
我尝试的是:
d_jlist = pd.read_csv('127case.csv', sep=',')
d_jlist = d_jlist.iloc[0:10,0:10]
d_jlist4 = d_jlist
d_jlist3 = pd.DataFrame()
a = np.random.choice(range(2,8),size = 5 ) # Randomly select 5 rows from existing rows
for i in a:
b = np.random.uniform(-1,1)
print(b)
d_jlist3 = d_jlist3.append(d_jlist4.iloc[i] ) + b #broadcast the random number to every element in this row
#d_jlist3 = d_jlist3.append(d_jlist4.iloc[i] + b ) # If I try in this way, why it will also change the existing rows?
print((d_jlist3))
d_jlist4 = d_jlist4.append(d_jlist3) #Add the new rows to the existing ones
生成的统一随机数和新行如下所示:
0.28761993446482825
-0.15132898721462507
0.8753189320820596
-0.05574300352910355
-0.7961990667560808
0:00 0:30 1:00 ... 3:30 4:00 4:30
6 -1.117332 -1.568332 -1.293332 ... -0.824332 -0.519332 -0.085332
6 -1.404952 -1.855952 -1.580952 ... -1.111952 -0.806952 -0.372952
7 -1.257623 -1.378623 -1.467623 ... -0.776623 -0.611623 -1.330623
5 -2.094942 -2.157942 -2.206942 ... -2.126942 -1.790942 -1.530942
4 -1.971199 -2.485199 -2.301199 ... -1.532199 -2.448199 -0.881199
我认为关系应该是旧行 + 随机数 = 新行。
但我检查了结果,它不能满足。我想知道实现这个?
提前致谢!
解决方案
这是一种方法(假设我了解您要执行的操作):
NUM_NEW_ROWS = 2
random_rows = df.sample(NUM_NEW_ROWS)
random_rows = random_rows.add(np.random.rand(NUM_NEW_ROWS), axis = "rows")
pd.concat([df, random_rows], axis="rows")
这是具有三列和值 1-9 的简单数据框的结果:
a b c
0 1.000000 4.000000 7.000000
1 2.000000 5.000000 8.000000
2 3.000000 6.000000 9.000000
0 1.415359 4.415359 7.415359
1 2.512821 5.512821 8.512821
推荐阅读
- python - 用于获取视频标题而不是下载视频的 youtube-dl 选项
- python - 如何在 django 中的 models.py 中的下拉列表中显示特定结果
- image - 如何拆分augmentedImageDatastore?
- c# - SendMessage,正在尝试发送 F4
- scala - 如何将comonad和monad组合成comonad?
- flutter - 参数类型“BoxShadow”不能分配给参数类型“列表”
' - c# - 详细视图给出错误对象引用未设置为 asp.net mvc 中的对象?
- javascript - javascript fetch 永远不会到达 nodejs 服务器上的 POST 请求
- c++ - 快速字符串解析器 C++
- c++ - CreateDepthStencilView() 失败