first time posting
working on this project in BigQuery Where I want to round of the weight slab in multiples of 0.5KG
For example
0.4KG round it of to 0.5KG
2.1KG THEN round it of to 2.5KG
even if it is 50 or 100 grams above than the current weight slab than I want it to round it of to the next weight slab
I tried
WHEN WKG.Weight_KG<=0.5 THEN WKG.Weight_KG=0.5
but the output comes in boolean format
even tried this
WHEN WKG.Weight_KG<=0.5 THEN ROUND(WKG.Weight_KG/ .5,0) * .5
but few of the numbers were rounded of to 0.0 instead of 0.5
This should work
case when round(weight * 10) % 10 = 0 then round(weight) else case when round(weight * 10) % 10 >= 5 then round(weight) + 1 else round(weight) + 0.5 end end as weight
You can use
TRUNC(value) + CEIL(MOD(value, 1.0) / 0.5) * 0.5
For instance,
SELECT value, TRUNC(value) + CEIL(MOD(value, 1.0) / 0.5) * 0.5 AS rounded_up
FROM UNNEST(generate_array(CAST(-0.6 AS NUMERIC), 2.6, 0.1)) AS value
returns
value rounded_up
-0.6 -0.5
-0.5 -0.5
-0.4 0
-0.3 0
-0.2 0
-0.1 0
0 0
0.1 0.5
0.2 0.5
0.3 0.5
0.4 0.5
0.5 0.5
0.6 1
0.7 1
0.8 1
0.9 1
1 1
1.1 1.5
1.2 1.5
1.3 1.5
1.4 1.5
1.5 1.5
1.6 2
1.7 2
1.8 2
1.9 2
2 2
2.1 2.5
2.2 2.5
2.3 2.5
2.4 2.5
2.5 2.5
2.6 3
Related
I have a dataframe with different id and possible overlapping time with the time step of 0.4 second. I would like to resample the average speed for each id with the time step of 0.8 second.
time id speed
0 0.0 1 0
1 0.4 1 3
2 0.8 1 6
3 1.2 1 9
4 0.8 2 12
5 1.2 2 15
6 1.6 2 18
An example can be created by the following code
x = np.hstack((np.array([1] * 10), np.array([3] * 15)))
a = np.arange(10)*0.4
b = np.arange(15)*0.4 + 2
t = np.hstack((a, b))
df = pd.DataFrame({"time": t, "id": x})
df["speed"] = pd.DataFrame(np.arange(25) * 3)
The time column is transferred to datetime type by
df["re_time"] = pd.to_datetime(df["time"], unit='s')
Try with groupby:
block_size = int(0.8//0.4)
blocks = df.groupby('id').cumcount() // block_size
df.groupby(['id',blocks]).agg({'time':'first', 'speed':'mean'})
Output:
time speed
id
1 0 0.0 1.5
1 0.8 7.5
2 1.6 13.5
3 2.4 19.5
4 3.2 25.5
3 0 2.0 31.5
1 2.8 37.5
2 3.6 43.5
3 4.4 49.5
4 5.2 55.5
5 6.0 61.5
6 6.8 67.5
7 7.6 72.0
I have a data frame as shown below
B_ID Session no_show cumulative_no_show u_no_show
1 s1 0.4 0.4 0.4
2 s1 0.6 1.0 1.0
3 s1 0.2 1.2 0.2
4 s1 0.1 1.3 0.3
5 s1 0.4 1.7 0.7
6 s1 0.2 1.9 0.9
7 s1 0.3 2.2 0.2
10 s2 0.3 0.3 0.3
11 s2 0.4 0.7 0.7
12 s2 0.3 1.0 1.0
13 s2 0.6 1.6 0.6
14 s2 0.2 1.8 1.8
15 s2 0.5 2.3 0.3
From the above I woulk like to estimate new column slot_num depends on u_no_show as explained below. if u_no_show increases increase slot_num by one else keep it as same.
Expected Output
B_ID Session no_show cumulative_no_show u_no_show slot_num
1 s1 0.4 0.4 0.4 1
2 s1 0.6 1.0 1.0 2
3 s1 0.2 1.2 0.2 2
4 s1 0.1 1.3 0.3 3
5 s1 0.4 1.7 0.7 4
6 s1 0.2 1.9 0.9 5
7 s1 0.3 2.2 0.2 5
10 s2 0.3 0.3 0.3 1
11 s2 0.4 0.7 0.7 2
12 s2 0.3 1.0 1.0 3
13 s2 0.6 1.6 0.6 3
14 s2 0.2 1.8 0.8 4
15 s2 0.5 2.3 0.3 4
I would do with two groupby:
s = df.groupby('Session').u_no_show.diff().gt(0).astype(int)
df['slot_num'] = s.groupby(df.Session).cumsum().add(1)
Output:
B_ID Session no_show cumulative_no_show u_no_show slot_num
0 1 s1 0.4 0.4 0.4 1
1 2 s1 0.6 1.0 1.0 2
2 3 s1 0.2 1.2 0.2 2
3 4 s1 0.1 1.3 0.3 3
4 5 s1 0.4 1.7 0.7 4
5 6 s1 0.2 1.9 0.9 5
6 7 s1 0.3 2.2 0.2 5
7 10 s2 0.3 0.3 0.3 1
8 11 s2 0.4 0.7 0.7 2
9 12 s2 0.3 1.0 1.0 3
10 13 s2 0.6 1.6 0.6 3
11 14 s2 0.2 1.8 1.8 4
12 15 s2 0.5 2.3 0.3 4
Ok, as a python beginner I found multiplication matrix in pandas dataframes is very difficult to conduct.
I have two tables look like:
df1
Id lifetime 0 1 2 3 4 5 .... 30
0 1 4 0.1 0.2 0.1 0.4 0.5 0.4... 0.2
1 2 7 0.3 0.2 0.5 0.4 0.5 0.4... 0.2
2 3 8 0.5 0.2 0.1 0.4 0.5 0.4... 0.6
.......
9 6 10 0.3 0.2 0.5 0.4 0.5 0.4... 0.2
df2
Group lifetime 0 1 2 3 4 5 .... 30
0 2 4 0.9 0.8 0.9 0.8 0.8 0.8... 0.9
1 2 7 0.8 0.9 0.9 0.9 0.8 0.8... 0.9
2 3 8 0.9 0.7 0.8 0.8 0.9 0.9... 0.9
.......
9 5 10 0.8 0.9 0.7 0.7 0.9 0.7... 0.9
I want to perform excel's sumproduct function in my codes and the length of the columns that need to be summed are based on the lifetime in column 1 of both dfs, e,g.,
for row 0 in df1&df2, lifetime=4:
sumproduct(df1 row 0 from column 0 to column 3,
df2 row 0 from column 0 to column 3)
for row 1 in df1&df2, lifetime=7
sumproduct(df1 row 2 from column 0 to column 6,
df2 row 2 from column 0 to column 6)
.......
How can I do this?
You can use .iloc to access row and columns with integers.
So where lifetime==4 is row 0, and if you count the column numbers where Id is zero, then column labeled as 0 would be 2, and column labeled as 3 would be 5, to get that interval you would enter 2:6.
Once you get the correct data in both data frames with .iloc[0,2:6], you run np.dot
See below:
import numpy as np
np.dot(df1.iloc[0,2:6], df2.iloc[1,2:6])
Just to make sure you have the right data, try just running
df1.iloc[0,2:6]
Then try the np.dot product. You can read up on "pandas iloc" and "slicing" for more info.
I am finding this issue quite complex:
I have the following df:
values_1 values_2 values_3 id name
0.1 0.2 0.3 1 AAAA_living_thing
0.1 0.2 0.3 1 AAA_mammals
0.1 0.2 0.3 1 AA_dog
0.2 0.4 0.6 2 AAAA_living_thing
0.2 0.4 0.6 2 AAA_something
0.2 0.4 0.6 2 AA_dog
The ouput should be:
values_1 values_2 values_3 id name
0.3 0.6 0.9 3 AAAA_living_thing
0.1 0.2 0.3 1 AAA_mammals
0.1 0.2 0.3 1 AA_dog
0.2 0.4 0.6 2 AAA_something
0.2 0.4 0.6 2 AA_dog
It would be like a group_by().sum() but only the AAAA_living_thing as the rows below are childs of AAAA_living_thing
Seperate the dataframe first by using query and getting the rows only with AAAA_living_thing and without. Then use groupby and finally concat them back together:
temp = df.query('name.str.startswith("AAAA")').groupby('name', as_index=False).sum()
temp2 = df.query('~name.str.startswith("AAAA")')
final = pd.concat([temp, temp2])
Output
id name values_1 values_2 values_3
0 3 AAAA_living_thing 0.3 0.6 0.9
1 1 AAA_mammals 0.1 0.2 0.3
2 1 AA_dog 0.1 0.2 0.3
4 2 AAA_something 0.2 0.4 0.6
5 2 AA_dog 0.2 0.4 0.6
Another way would be to make a unique identifier for rows which are not AAAA_living_thing with np.where and then groupby on name + unique identifier:
s = np.where(df['name'].str.startswith('AAAA'), 0, df.index)
final = df.groupby(['name', s], as_index=False).sum()
Output
name values_1 values_2 values_3 id
0 AAAA_living_thing 0.3 0.6 0.9 3
1 AAA_mammals 0.1 0.2 0.3 1
2 AAA_something 0.2 0.4 0.6 2
3 AA_dog 0.1 0.2 0.3 1
4 AA_dog 0.2 0.4 0.6 2
Is there a straightforward existing method to apply a function at fixed interval with pandas (or numpy, scipy) ?
Example
A pd.DataFrame of length 11
0 0.2
1 0.3
2 0.4
3 0.4
4 0.4
5 0.4
6 0.4
7 0.4
8 0.4
9 0.4
10 0.6
For instance applying a min function with interval = 5 would result in
0 0.2 # Beginning of interval
1 0.2
2 0.2
3 0.2
4 0.2 # End of interval
5 0.4 # Beginning of interval
6 0.4
7 0.4
8 0.4
9 0.4 # End of interval
10 0.6 # Beginning of interval (takes the min function of the remaining values)
So far I can do it with
df = pd.read_clipboard(index_col = 0, header = None) # Copying the above data
df['intervals'] = (np.arange(len(df)) / 5).astype(int)
mapper = df.groupby('intervals').min()
result = df['intervals'].apply(lambda x: mapper.loc[x])
print result
But I wonder if there exists fixed interval filters already built in pandas/numpy/scipy.
One of the various possibilities would be to use groupby.transform after grouping them as per the necessary window interval.
When you perform min on the transform method of groupby, all sub-groups would get filled by the smallest value present in their respective group.
Assuming the single columned DF to be represented by s:
s.groupby(np.arange(len(s.index)) // 5).transform('min')
produces:
0 0.2
1 0.2
2 0.2
3 0.2
4 0.2
5 0.4
6 0.4
7 0.4
8 0.4
9 0.4
10 0.6
dtype: float64