Index pandas dataframe rows 4 at a time [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to to be able to something along the lines of:
for i in range(0, len(df), 4):
curr = pd.DataFrame()
vcch = int(df.loc[i, 'IN_CUSTOM_SELECT'])
icch = int(df.loc[i+1, 'IN_CUSTOM_SELECT'])
vccl = int(df.loc[i+2, 'IN_CUSTOM_SELECT'])
iccl = int(df.loc[i+3, 'IN_CUSTOM_SELECT'])
idlpwr = (vcch * icch) + (vccl * iccl)
idlpwr = idlpwr / (10**6)
where I do some calculations based on the specific values of columns in combinations of rows of 4.

If you're just working with a regular autonumbered index, one easy option is to reshape your data and use pandas vectorized operations for the math:
In [196]: df = pd.DataFrame({'IN_CUSTOM_SELECT': np.random.random(24)})
In [197]: reshaped = df.set_index([df.index.map(lambda x: x // 4), df.index.map(lambda x: x % 4)]).unstack()['IN_CUSTOM_SELECT']
In [198]: reshaped['idlpwr'] = ((reshaped[0] * reshaped[1]) + (reshaped[2] * reshaped[3])) / 10**6
In [199]: reshaped
Out[199]:
0 1 2 3 idlpwr
0 0.788758 0.853356 0.627796 0.355143 8.960487e-07
1 0.312111 0.602934 0.908984 0.046183 2.301622e-07
2 0.842201 0.507629 0.541432 0.592680 7.484218e-07
3 0.506601 0.605108 0.497627 0.362006 4.866923e-07
4 0.308097 0.991945 0.822433 0.272082 5.293851e-07
5 0.573716 0.852356 0.009606 0.961437 4.982462e-07

Related

Working with two Two-dimensional arrays in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
my_list = [['Chris',33,'JAN'],['Katia',40,'JAN'],['Petunia',54,'JAN'],['Clee',26,'JAN'],['katt',73,'JAN'],['battt',83,'JAN'],['FRIES',59,'FEB'],['GGEEZ',89,'FEB'],['SHEEESH',25,'MAR']]
threshold = [[217, 'JAN'], [104, 'FEB'], [18, 'MAR']]
output: [['Chris','Katia','Petunia','Clee','katt'],['FRIES','GGEEZ'],['SHEEESH']]
I want to make a new list with the first element in the nested array (the names) until the sum of the second elements in the nested array passes the 217 for JAN, 104 for FEB and 18 for MARCH.
I dont know how to do it since both of the lists are are indented and I find that hard to work with, But it should check it in a loop if my_list[2] == threshold[1] and sum the my_list[1]s until it is greater or equal to threshold[0] than it should go and check if the and check if my_list[2] == threshold[1] (but this time we skip the remaining januaries and check if the february is equal to the mylist and so on, its hard to articulate
Try:
my_list = [['Chris',33,'JAN'],['Katia',40,'JAN'],['Petunia',54,'JAN'],['Clee',26,'JAN'],['katt',73,'JAN'],['battt',83,'JAN'],['FRIES',59,'FEB'],['GGEEZ',89,'FEB'],['SHEEESH',25,'MAR']]
threshold = [[217, 'JAN'], [104, 'FEB'], [18, 'MAR']]
results = []
for max_num, month in threshold:
accumulator = []
count = 0
for s, num, month_ in my_list:
if month == month_ and count < max_num:
accumulator.append(s)
results.append(accumulator)
print(results)
output:
[['Chris', 'Katia', 'Petunia', 'Clee', 'katt', 'battt'], ['FRIES', 'GGEEZ'], ['SHEEESH']]
output = []
for a,b in threshold:
sum = 0
curr = []
for x,y,z in my_list:
if z == b and sum < a:
sum += y
curr.append(x)
output.append(curr)

Find row in array corresponding to combination of numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working with q-tables and I got 30 * 30 * 30 * 30=810000 different states. I want a function that takes for example [10, 20, 16, 5] as input and this should correspond to a particular row representing this exact combination of state but I cannot figure out how to do that. One (bad) approach would be 10 * 20 * 15 * 5=15000 but then row number 15 000 would also be represented by 20 * 10 * 5 * 15 = 15000 and I do not want that. How can I make this work?
One way would be to represent your table as a 4-dimensional list:
self.q_table = [[[[0 for _ in range(30)] for _ in range(30)] for _ in range(30)] for _ in range(30)]
so you can do:
def func(self, coords):
[q, r, s, t] = coords
entry = self.q_table[q][r][s][t]
...
If you wanted to flatten it to a single-dimensional list, then you need to multiple each coordinate by an increasing power of 30 to make sure each combination of coordinates yields a unique result:
self.q_table = [0 for _ in range(30**4)]
def func(self, coords):
row_num = sum(i * 30**p for p, i in enumerate(coords))
entry = self.q_table[row_num]
...

Loop the below way in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a column 'Distance' with value from 0 to n. I want to write a loop such that, if distance is above 0.5km it should say M1. When it is less than 0.5, it should be H1. When it crosses 0.5 again it should give M2.
My dataset:
Expected output:
How can i do this?
Here is an algorithm to get you started. Improve it to suit your needs
df = pd.read_csv("input.csv")
m_count = 0
h_count = 0
current = "H"
status_halt = []
for idx in df.index:
if df["Distance_km"][idx] < 0.5:
if current == "M":
h_count += 1
status_halt.append(f"H{h_count}")
current = "H"
elif df["Distance_km"][idx] > 0.5:
if current == "H":
m_count += 1
status_halt.append(f"M{m_count}")
current = "M"
df["Status_halt"] = status_halt

How to combine numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
x = 5
y = 8
I want to combine those variables and define an another variable, like:
z = 58
It depends on what you mean by "combine".
You can concatenate numbers as strings:
z = int(str(x) + str(y))
But you can also compute x * 10 + y:
z = x * 10 + y
This will give different results if y > 9, e.g. for x = 5 and y = 10, the first version will give 510, while the second version will give 60.
Convert them to strings and concatenate them, then convert them back to an integer:
z = int(str(x) + str(y))
If you're using Python 3.6 or later, this can be done quite concisely with format strings:
>>> x = 5
>>> y = 8
>>> z = int(f'{x}{y}')
>>> z
58
>>>
A more general solution would be something like:
>>> def join_ints(*args):
... return int(''.join(map(str, args)))
...
>>> join_ints(5, 6, 8, 3)
5683
>>>

Name and function for the following matrix operation in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to reproduce this [50 x 50] matrix generated with Python as:
n = 50
a = np.linspace(-5, 5, n).reshape(-1,1)
b = a
np.sum(a**2, 1).reshape(-1, 1) + np.sum(b**2, 1)
using R. The problem is that the result is some sort of matrix, which cannot be reproduced through:
n = 50
a = seq(-5, 5, length.out = n)
b = a
a^2 + b^2
which generates a vector.
I am not familiar with the object names in Python, but I see that np.sum(a**2, 1).reshape(-1, 1) produces what looks like a [50 x 1] column vector:
array([[ 2.50000000e+01],
[ 2.30008330e+01],
...
[ 2.10849646e+01],
[ 2.30008330e+01],
[ 2.50000000e+01]])
while np.sum(b**2, 1):
array([ 2.50000000e+01, 2.30008330e+01, 2.10849646e+01,
1.92523948e+01, 1.75031237e+01, 1.58371512e+01,
...
1.27551020e+01, 1.42544773e+01, 1.58371512e+01,
1.75031237e+01, 1.92523948e+01, 2.10849646e+01,
2.30008330e+01, 2.50000000e+01])
looks like the transposed of that same vector. So we have an operation of the form [50 x 1] * [1 x 50] = [50 x 50].
What is the generic name of this operation? And how can I reproduce it in R?
You are looking for ?outer I believe. As per the help file, it returns:
The outer product of the arrays X and Y ... the array A with dimension
c(dim(X), dim(Y))
So, for your specific example, try:
outer(a^2,b^2,FUN=`+`)
# [,1] [,2] [,3]
#[1,] 50.00000 48.00083 46.08496 ...to col 50
#[2,] 48.00083 46.00167 44.08580 ...to col 50
#[3,] 46.08496 44.08580 42.16993 ...to col 50
# ...to row 50

Categories