I have a various columns with numeric data in them, and I was wondering if I can somehow switch values between columns IF a condition is met - if value in columna A is equal to 0 and value in column B is deifferent that 0, then I would like to swich those values so that column A has a value from B and vice versa.
I was trying to do that with Table.ReplaceValue but the problem is, that once I replace a value in column A with that from column B, my condition won't be met during next replacement.
Example:
If a Table looks like that:
PART NO
COLUMN A
COLUMN B
1
120
0
2
0
80
3
130
140
I'd like it to change like this:
PART NO
COLUMN A
COLUMN B
1
120
0
2
80
0
3
130
140
Add column .. custom column... Column A.1
= if (insert your test here) then [COLUMN B] else [COLUMN A]
Add column .. custom column... Column B.1
= if [COLUMN A] = [COLUMN A.1] then [COLUMN B] else [COLUMN A]
then right click and remove original two columns, and rename these
I need some help to determine the best approach on how I can use VBA to find duplicates in a column 2 and swap the rows that have the duplicates in Column 2 (in this example the row 5 with row 6). Please see below sample.
Column 1 Column 2 Column 3
A 1 yes
B 2 no
C 3 no
D 4 yes
E 5 no
F 5 yes
I'm trying to generate new rows based on values in a certain column. In current data as you can see 'days_left' column does not have all sequential values.
current = {'assignment': [1,1,1,1,2,2,2,2,2], 'days_left': [1, 2, 5, 9,1, 3, 4, 8, 13]}
dfcurrent = pd.DataFrame(data=current)
dfcurrent
While I want to generate rows into that dataframe to create make sequential list for for 'days_left' for each 'assignment'. Please see the desidered output below:
desired = {'assignment': [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2],
'days_left': [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,11,12,13]}
dfdesired = pd.DataFrame(data=desired)
dfdesired
Note: The original data is much bigger and has other columns as well but I just simplified it for this question.
Could you please help me how I can solve this?
Thank you very much in advance!
You can iterate through the rows of the current dataframe and create a new dataframe. For each days_left range, copy the current row to the new dataframe and update the days_left column value.
Try this code:
import pandas as pd
current = {'assignment': [1,1,1,1,2,2,2,2,2], 'days_left': [1, 2, 5, 9, 1, 3, 4, 8, 13]}
dfc = pd.DataFrame(data=current)
dfd = pd.DataFrame() # new dataframe
for r in range(1,len(dfc)): # start at 2nd row
for i in range(dfc.iloc[r-1]['days_left'],dfc.iloc[r]['days_left']): # fill gap of missing numbers
dfd = dfd.append(dfc.iloc[r]) # copy row
dfd.reset_index(drop=True, inplace=True) # prevent index duplication
dfd.loc[len(dfd)-1, 'days_left'] = i # update column value
if r == len(dfc)-1 or dfc.iloc[r+1]['assignment']!=dfc.iloc[r]['assignment']: # last entry in assignment
dfd = dfd.append(dfc.iloc[r]) # copy row
dfd.reset_index(drop=True, inplace=True) # prevent index duplication
dfd = dfd.astype(int) # convert all data to integers
print(dfd.to_string(index=False))
Output
assignment days_left
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
The problem statement goes like this:
Given an N x M array of (nonnegative) integers, find the optimal value of each column in the array, taking into account free rows.
A free row is anything in the range of [prevOld, prevNew]
Free rows are given starting values of:
prevOld = 0
prevNew = 0
so at the first step, only row 0 is free. If an element lies in a free row, it incurs no penalty.
If an element is not in a free row, then the penalty incurred is 2 * distance to closest free row - (e.g if free rows are [1,3] and our element is in row 5 then the element loses 2*(5-3) value. But if the element was in row 2, then no pentalty is incurred since 1 <= 2 <= 3
Once an element has been selected, free rows are updated as such:
prevOld = prevNew
prevNew = row of selected element
So if we begin with [x,y] and choose element in row z, then for the next column, free rows are now [y,z]
We are asked to solve this using an dynamic programming algorithm.
I am having a hell of a time coming up with a recurrence relation for this problem. I originally tried an algorithm that chooses the maximum element in a column based on a "real" value given the free rows, but this doesn't take into account the fact that sometimes we want to choose a lower value in our current column for a higher value in the next column. Any point in the right direction would be greatly appreciated.
EDIT
Sample input/output (no input/output was given, so putting this together from instructions):
Input: 3 x 3 array
4 5 7
7 8 7
1 9 10
First column is 4 7 1 and our starting rows are prevOld = 0 and prevNew = 0, [0,0]
so the 0th row is the only free row, with that, the "real" values of the first column are: 4 5 -3
4 is row 0, so it is free therefore its value is not affected
7 is in row 1, which is 1 away from the closest free row 0, so it loses 2*1 value, so 7 has a "real" value of 5
1 is in row 2, which is 2 away from the closest row, so it loses 2*2 value, so 1 has a "real" value of -3
With these real values we would select 7 (row 1) as our choice. Now we update prevOld and prevNew
prevOld = prevNew
prevNew = 1 (because we selected row 1)
so now we have [0,1] as the free rows and we move onto the next column: 5 8 9
Skipping ahead: real values of this column are
5 (row 0 is free, no loss)
8 (row 1 is free, no loss)
7 (row 2 is 1 away from the closest free row, so it loses 2*1 value)
so we choose 8 (row 1) and update prevOld and prevNew again:
prevOld = prevNew
prevNew = 1 (selected row)
free rows are now [1,1] for the final column: 7 7 10
real values are: 5 7 8, so we choose 8 and were done
Output is: 1 1 2 (the rows we selected in each column), total: 21 (total of the "real" values we selected in the rows)
I have a table that's like this
rank continuationofrow
1 row
2 row
3 row
4 row
4 row
4 row
I'm trying to identify the previous rows rank number within an Oracle statement. Any help is greatly appreciated. I've searched the internet and haven't found much.
You must have another column that establishes the order of the rows with the same rank, otherwise the concept of "previous row" is meaningless. Let's suppose you do:
seq rank continuationofrow
1 1 row
2 2 row
3 3 row
4 4 row
5 4 row
6 4 row
No you can use an analytic function:
select seq, rank, continuationofrow, lag(rank) over (order by seq) as prev_rank
from mytable;
seq rank continuationofrow prev_rank
1 1 row
2 2 row 1
3 3 row 2
4 4 row 3
5 4 row 4
6 4 row 4
select
...
lag(rank, 1) over (order by ordering-columns)
from
..