寻找数据框架中特定X值处的一系列Y值

huangapple go评论94阅读模式
英文:

Finding a range of Y-values at specific X-value across dataframes

问题

You can calculate the range of Y-values around x = 1.0 for each dataframe by interpolating the values at x = 1.0. Here's how you can do it in Python using pandas and interpolation:

  1. import pandas as pd
  2. # Define your dataframes (dataframe1, dataframe2, etc.)
  3. # Define the target x value
  4. target_x = 1.0
  5. # Create a list to store the interpolated Y-values for each dataframe
  6. y_ranges = []
  7. # Iterate through your dataframes
  8. for df in [dataframe1, dataframe2, dataframe3]:
  9. # Use interpolation to find the Y-value at the target_x
  10. interpolated_y = df['Y'].interpolate(method='linear', limit_direction='both').loc[target_x]
  11. y_ranges.append(interpolated_y)
  12. # Calculate the range of Y-values
  13. y_range = max(y_ranges) - min(y_ranges)
  14. # Print the range of Y-values
  15. print(f"Range of Y-values around x = {target_x}: {y_range}")

This code will give you the range of Y-values around x = 1.0 across your dataframes. You can extend this code to include all 10 of your dataframes.

英文:

I am trying to find a quick way of finding a range of Y-values across a set of dataframes that I have. Here is what my dataframes look like:

  1. #Example dataframe1
  2. X Y
  3. 0 0
  4. 0.2 27
  5. 0.4 31
  6. 0.6 34
  7. 0.8 39
  8. 1.0 43
  9. 1.2 44
  10. #Example dataframe2
  11. X Y
  12. 0 0
  13. 0.2 12
  14. 0.3 14
  15. 0.4 18
  16. 0.5 21
  17. 0.6 22
  18. 0.7 24
  19. 0.8 29
  20. 0.9 34
  21. 1.0 38
  22. 1.1 40
  23. #Example dataframe3
  24. X Y
  25. 0 0
  26. 0.2 19
  27. 0.3 24
  28. 0.4 29
  29. 0.5 34
  30. 0.6 38
  31. 0.7 39
  32. 0.8 47
  33. 0.9 51
  34. 1.0 52
  35. 1.1 56
  36. X1 0.00000000 0.00000000
  37. X2 0.00644777 -3.74942549
  38. X3 0.01250601 -1.64198832
  39. X4 0.01746194 -3.09841231
  40. X5 0.02656210 -3.24436861
  41. X6 0.03360072 1.92721788
  42. X7 0.04399755 -4.38984054
  43. X8 0.05404601 -1.07421099
  44. X9 0.06154399 1.05752545
  45. X10 0.07105423 -2.64811489
  46. X11 0.08315538 -2.10245766
  47. X12 0.08925389 -1.13184408
  48. X13 0.10060635 0.51200462
  49. X14 0.10833009 4.54644990
  50. X15 0.12196157 -3.08377282
  51. X16 0.13730928 -0.27015071
  52. X17 0.14890850 -0.65513113
  53. X18 0.16087072 2.17034586
  54. X19 0.17361501 -0.93398437
  55. X20 0.18310140 0.79111051
  56. X21 0.19810085 -3.87227906
  57. X22 0.21316248 2.13154747
  58. X23 0.22423607 1.36416314
  59. X24 0.23529990 4.99793424
  60. X25 0.24695577 -1.01918599
  61. X26 0.26119599 2.93076793
  62. X27 0.27643532 2.91155218
  63. X28 0.28517213 3.57100964
  64. X29 0.29978970 -0.73012874
  65. X30 0.31870887 -2.78233188
  66. X31 0.33418021 -1.51922907
  67. X32 0.34585044 2.87420871
  68. X33 0.36071229 1.14786764
  69. X34 0.37379062 2.99504234
  70. X35 0.39324498 0.63168705
  71. X36 0.41099992 -0.07084714
  72. X37 0.42437476 1.54668167
  73. X38 0.43643925 2.81198973
  74. X39 0.44687960 4.92643076
  75. X40 0.45864752 4.11311344
  76. X41 0.48055932 4.39500995
  77. X42 0.49634662 7.15432395
  78. X43 0.50779009 8.58246880
  79. X44 0.52861303 2.82337466
  80. X45 0.54971600 2.99739656
  81. X46 0.56138206 6.78068642
  82. X47 0.57472491 4.45406406
  83. X48 0.59202033 2.46356370
  84. X49 0.60975420 3.15161790
  85. X50 0.63057494 2.34243629
  86. X51 0.64653736 2.56960396
  87. X52 0.66088217 11.82388880
  88. X53 0.68168259 6.09572345
  89. X54 0.69649595 5.09381202
  90. X55 0.71363837 4.46075825
  91. X56 0.72988760 8.62040355
  92. X57 0.75010443 6.83203133
  93. X58 0.76776582 9.18985022
  94. X59 0.78613955 7.86350629
  95. X60 0.81099886 6.03907061
  96. X61 0.82979667 9.34560816
  97. X62 0.84431553 9.42604561
  98. X63 0.86824787 7.64843057
  99. X64 0.88447672 6.16947933
  100. X65 0.89909577 9.87215457
  101. X66 0.92162514 10.98250589
  102. X67 0.94854647 5.26614443
  103. X68 0.96631038 8.94057689
  104. X69 0.98120815 14.91703428
  105. X70 1.00357985 11.66179411
  106. X71 1.02769291 8.30042153
  107. X72 1.04733002 7.64827230
  108. X73 1.06585240 10.99394148
  109. X74 1.09083056 7.14363152
  110. X75 1.11592567 5.93705238
  111. X76 1.13627303 10.89649169
  112. X77 1.15866983 8.08089986
  113. X78 1.17941856 10.63699550
  114. X79 1.19748724 12.61902305
  115. X80 1.22012913 11.83730809
  116. X81 1.24142718 9.53528978
  117. X82 1.26937973 9.20448095
  118. X83 1.29283488 6.75640546
  119. X84 1.31011736 15.04334314
  120. X85 1.33134723 12.18910484
  121. X86 1.36014366 11.80276542
  122. X87 1.38395822 14.18290101
  123. X88 1.40615380 13.99689728
  124. X89 1.42895305 12.04541339
  125. X90 1.45046842 10.61135555
  126. X91 1.47504234 11.07833983
  127. X92 1.49920797 12.37669790
  128. X93 1.52142668 14.35112239
  129. X94 1.54559219 16.42659881
  130. X95 1.57354927 13.69037310
  131. X96 1.60000896 13.51107220
  132. X97 1.62449229 13.96191530
  133. X98 1.64506662 16.74995717
  134. X99 1.67100036 14.67649138
  135. X100 1.70185161 10.71288999
  136. X101 1.72964287 14.56576519
  137. X102 1.75235510 8.66232995
  138. X103 1.77455521 18.00492491
  139. X104 1.80183053 18.36635863
  140. X105 1.83121216 12.63300200
  141. X106 1.85619974 15.88991904
  142. X107 1.88188243 13.53745701
  143. X108 1.91002214 18.05483899
  144. X109 1.93333006 18.40951264
  145. X110 1.96133912 15.26804166
  146. X111 1.98944890 15.16029048
  147. X112 2.01225924 19.62616569
  148. X113 2.03804183 19.73062246
  149. X114 2.06614304 15.57687649
  150. X115 2.09558916 19.55369508
  151. X116 2.12400699 17.90633921
  152. X117 2.14846969 21.81952636
  153. X118 2.18011498 16.62377098
  154. X119 2.20532560 18.47341439
  155. X120 2.23031592 25.90875455
  156. X121 2.26106715 16.58422332
  157. X122 2.28796983 23.44278876
  158. X123 2.31954741 19.14855256
  159. X124 2.35274339 18.75923949
  160. X125 2.37794161 20.01523111
  161. X126 2.40643740 24.42732689
  162. X127 2.43963051 21.06897103
  163. X128 2.46948838 16.13112984
  164. X129 2.49733877 24.40617463
  165. X130 2.53035021 20.97045518
  166. X131 2.56229091 18.55134741
  167. X132 2.59265327 20.27110860
  168. X133 2.61934137 21.31728543
  169. X134 2.64592481 21.69982327
  170. X135 2.67754769 28.79262127
  171. X136 2.70779777 20.45346529
  172. X137 2.73824191 24.89121411
  173. X138 2.76880598 29.23390805
  174. X139 2.80077982 23.31624195
  175. X140 2.83057237 21.14259882

Now, let's say that I want to determine the rate of increase at about x = 1.0, relative to the beginning of the period (i.e. 0 at the beginning in each case). We can see clearly here that the answer would be y = 43, y = 38 and y = 52 among the three cases, with 52 being the highest and 38 being the lowest (so, a range of 14). My question is would it be possible to quickly determine the value at around x = 1.0 across the 10 dataframes that I have like this without having to manually check them? The idea is to determine the range of values for y at around x = 1.0 across all dataframes. Note that it's not perfectly x = 1.0 in all dataframes - some could be something like 0.99 or 1.01.

Thank you, and I your feedback would be most appreciated!

答案1

得分: 1

  1. 你可以将所有的数据框进行`rbind`,然后进行子集筛选,使用`range`函数来获取`X`等于特定值的范围,接着使用`diff`函数计算你所需的数值。例如,在你的示例中:
  2. ```R
  3. ll <- rbind(df1, df2, df3)
  4. range(ll[ll$X == 1, "Y"])
  5. # [1] 38 52
  6. rr <- range(ll[ll$X == 1, "Y"], na.rm = TRUE)
  7. diff(rr)
  8. # [1] 14

你可以稍微扩展这个范围以涵盖1.0附近的值,创建一个最小值和最大值来腾出一些余地:

  1. minwiggle <- 0.99
  2. maxwiggle <- 1.01
  3. range(ll[ll$X <= maxwiggle & ll$X >= minwiggle, "Y"])
  4. # [1] 38 52
  5. rr <- range(ll[ll$X <= maxwiggle & ll$X >= minwiggle, "Y"], na.rm = TRUE)
  6. diff(rr)
  7. # [1] 14
  1. <details>
  2. <summary>英文:</summary>
  3. You could `rbind` all the data frames and then subset and use `range` where `X` equals, then `diff` to calculate your desired value. For instance, in your example:

ll <- rbind(df1, df2, df3)
range(ll[ll$X == 1, "Y"])

[1] 38 52

rr <- range(ll[ll$X == 1, "Y"], na.rm = TRUE)
diff(rr)

[1] 14

  1. You could expand this a little bit around the range of 1.0 by creating a min and max value for wiggle room:

minwiggle <- 0.99
maxwiggle <- 1.01

range(ll[ll$X <= maxwiggle & ll$X >= minwiggle, "Y"])

[1] 38 52

rr <- range(ll[ll$X <= maxwiggle & ll$X >= minwiggle, "Y"], na.rm = TRUE)
diff(rr)

[1] 14

  1. </details>
  2. # 答案2
  3. **得分**: 1
  4. Here is the translation of the provided code:
  5. ```text
  6. 一种方法:
  7. ```R
  8. target_val = 1 ## 要匹配的X值(尽可能接近)
  9. list(df_A, df_B, df_C) |&gt; ## 数据框列表
  10. Map(f = \(the_df) {
  11. ## 找到X最接近target_val的行索引
  12. row_index = which.min(abs(the_df$X - target_val))
  13. the_df$Y[row_index]
  14. }) |&gt;
  15. Reduce(f = c) |&gt; ## 连接值
  16. fivenum() ## 在这里放入聚合函数

对于X == 1,使用您的示例数据框的输出:

  1. [1] 38.0 40.5 43.0 47.5 52.0
  1. Please note that I've translated the code portion, as requested, and provided the code in a formatted manner.
  2. <details>
  3. <summary>英文:</summary>
  4. one approach:

target_val = 1 ## X value to match (as close as possible)

list(df_A, df_B, df_C) |> ## list of dataframes
Map(f = (the_df) {
## find row index where X is closest to the target_val
row_index = which.min(abs(the_df$X - target_val))
the_df$Y[row_index]
}) |>
Reduce(f = c) |> ## concatenate values
fivenum() ## put aggregating function here

  1. output for X == 1, using your example dataframes:

[1] 38.0 40.5 43.0 47.5 52.0

  1. </details>

huangapple
  • 本文由 发表于 2023年5月10日 20:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定