V1 V2
0.52
0.813
0.75
0.925
1.24
.
how to group V2 according to interval (statistical maximum to minimum, divided into n intervals), and count the average of all V1 values in each interval
V1 V2
0.52
0.813
0.75
0.925
1.24
.
how to group V2 according to interval (statistical maximum to minimum, divided into n intervals), and count the average of all V1 values in each interval
part of binning can be used with case_when
library(tidyverse)
data %>%
mutate(V2 = case_when(V2 %>% between(0,1) ~ "0-1",
V2 %>% between(1,2) ~ "0-2",
TRUE ~ ">=3") %>%
group_by(V2) %>%
summarize(mean_value = mean(V1))
whether the code of R is more user-friendly than the one above.
Group v2 columns, map them to grouped ID columns, and then do average statistics.
Please refer to the following code
'''
author:
'''
import numpy as np
import pandas as pd
df = pd.DataFrame([
[0.1, 1],
[0.2, 2],
[0.3, 3],
[0.4, 4],
[0.5, 5],
[0.6, 6],
], columns=['v1', 'v2'])
-sharp v2
-sharp np.histogram
ranges = np.histogram(df.v2.values, 3)[1]
def tag_v2(value, ranges):
''' ID v2 '''
for i in range(len(ranges) - 1):
if value >= ranges[i] and value <= ranges[i+1]:
return i
return -1
-sharp v2
df['v2_tag'] = df.v2.apply(lambda i: tag_v2(i, ranges))
print df
output results
v1 v2 v2_tag
0 0.1 1 0
1 0.2 2 0
2 0.3 3 1
3 0.4 4 1
4 0.5 5 2
5 0.6 6 2
Statistical average
print df.groupby('v2_tag')['v1'].mean()
output results
v2_tag
0 0.15
1 0.35
2 0.55
Name: v1, dtype: float64
use pandas.cut
and groupby
import pandas as pd
from io import StringIO
s = """
V1,V2
0.5,2
0.8,13
0.7,5
0.9,25
1.2,4"""
df = pd.read_csv(StringIO(s))
-sharp 10
step = (df.V2.max - df.V2.min)/10
bins = [df.V2.min() + i * step for i in range(11)]
result = df.groupby(pd.cut(df.V2, bins)).V1.mean()
output:
V2
(2.0, 4.3] 1.2
(4.3, 6.6] 0.7
(6.6, 8.9] NaN
(8.9, 11.2] NaN
(11.2, 13.5] 0.8
(13.5, 15.8] NaN
(15.8, 18.1] NaN
(18.1, 20.4] NaN
(20.4, 22.7] NaN
(22.7, 25.0] 0.9
Name: V1, dtype: float64
Previous: How to open input? through js
Next: How does mongodb combine several collections to query together
use reg = (? < = mileage). (re.findall: km | km (?! ) | (em: km | km (?! ). (? = mileage) the method is re.findall the text above is input the list below is output for example, for the first one, I only need to take 34km , for example, fo...
send a request to oppo with the following headers POST client get_search_completion.pb HTTP 1.1 brand: samsung rom: 2 desktop: desktop_other locale: zh_CN uid: oppo.uid.nearme Referer: SM-G955N appstore 4.3.1 354730010542250 User-Agent: SM-G955N 4.4.2...
The expression in the question is a little unclear: specifically, the reason is a malfunction of the CPU circuit board and needs to be replaced. B= CPU circuit board import re C = re.search (A Magi B) print (C) how to get the index value of CPU c...
assume that all entity names are stored in the A.xlxs (to add, hundreds of entities, tens of thousands of sequences), and assume that one of the sequences reads: "there are bad spots on the screen visible on the scene, the sound of the handset is too ...
Hello, folks, ask if there is a tool that can monitor the status of crawler processes, such as running, stopping, number of processes, data quality, log collection, etc. preferably in the web interface. my crawlers are written by requests+ multi-proces...
I successfully installed a library on the command line of raspberry pie with the pip command, but could not introduce that library in IDE thonny, and the package manager only had the most basic libraries and did not show the newly installed ones. what...
What should I do if the string is split into two parts in front of parentheses and in parentheses? for example, ABC1.1 (1) is split into ABC1.1 and 1 % DEF2.1 (3) is split into% DEF2.1 and 3 @ I GHI3.3 O GHI3.3 (5) is split into @ I GHI3.3 O GHI3.3 an...
how does requests-html get the innerText of a tag def stage_catch(): session = HTMLSession() r = session.get( http: www.dmzx.com manhua 358 ) div = r.html.find( .subsrbelist ) list_a = div[0].find( ul )[0].find( a ) -sha...
< span data-original-name= < Duke University Blue Devil Men s Basketball team > Duke University Blue Devil Men s Basketball team < span data-original-name= "unknown " > unknown how to get the Duke University Blue Devil Men s Basketball team out...
after simulating a successful login, when a jsp page is requested using python3.6 s requests library, there is no response for a long time. finally reports an error: ( Connection aborted. , RemoteDisconnected ( Remote end closed connection witho...
for example: the newly released Samsung Galaxy S9 features as follows: 1. The Exynos9810 processor is adopted, and the 2.RAM is 4GB. The battery capacity is 3000mAh. expect to extract the S9 Magi Exynos 9810 GB mAh. I hope you can give me some advice o...
ret=re.search (r dudes) print (ret.group ()) results do not meet expectations: 999 ...
After thinking about it for a long time, I really can t understand how locks are used in listing 6-9. Locks are only set with a key, and are not used at all (such as if getkey () = = true and so on). The generated locks are still random, that is, the co...
str = "http: bbs.aa.cc avatar.php?uid=157135&size=large " " I want to match uid=157135, and the position where this parameter appears is not necessarily fixed. for example, put at the end: http: bbs.aa.cc avatar.php?s. or first ...
refers to a problem in offer, please implement a function to determine whether a string represents a numeric value (including integers and decimals). For example, the strings "+ 100 ", " 5e2 ", "-123 ", "3.1416 " and "- 1E-16 " all represent numeric...
how do you use Python to match the two parameter values in SetValue in the following string? for example, several pieces of data failed to match between "Att_Sys_zh-cn_11_G " and 12 12 5 . <script> function init(){ SetValue( txt_gtin , &...
python regular matches the contents of the specified start HTML tag and end tag <div class="pbm mbm bbda cl"> <h2 class="mbn">< h2> <a href="http: bbs.aa.cc home.php?mod=spacecp&ac=p...
The problem is as follows, as seen in the python getting started manual, re.sub () replaces I know the syntax of re.sub (regular string, replacement string, original string), but I don t understand this I also know that 1 is equivalent to re.sea...
not long after I first came into contact with python, I needed to use regularities when crawling pages, but I was confused after reading python s re library for a long time. Maybe I got silly after a whole day s work. =-sharp I have many of the follow...
problem description render () { return ( <div onClick={()=>{alert(1)}}> <div onClick={()=>{alert(2)}}>Hello< div> <NavLinkBar onClick={()=>{alert(3)}} data={navList}> < NavLinkBar> < di...