Value Character EBCDIC Hex code ASCII Hex code
+0 { C0 7B +1 A C1 41 +2 B C2 42 +3 C C3 43 +4 D C4 44 +5 E C5 45 +6 F C6 46 +7 G C7 47 +8 H C8 48 +9 I C9 49
-0 } D0 7D -1 J D1 4A -2 K D2 4B -3 L D3 4C -4 M D4 4D -5 N D5 4E -6 O D6 4F -7 P D7 50 -8 Q D8 51 -9 R D9 52
The last character needs to be replaced using the value above table. If last character is from first set, replace with value and retain sign. If last character is from second set, replace with value and use '-' sign for entire number. example: 100K -> -1002 1750B -> 17502 Need to change the last digit based on the table
object testTransformer extends testaTransformer[String] {
override def apply(args: String*): (String, test2ContributionRecord) = {
var validationError = validate(args)
var output: String = null
val positiveLastChar = List('}', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R')
val negativeLastChar = List('{', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I')
if (validationError == null) {
val input = args(0)
if (input == null || input.length == 0){
output = input
} else {
val lastChar = input.trim.charAt(input.trim.length - 1)
if (positiveLastChar.contains(lastChar.toUpper)) {
output = input.trim.dropRight(1)
} else if (negativeLastChar.contains(lastChar.toUpper)) {
output = '-' + input.trim.dropRight(1)
} else {
logger.error(s"Unexpected value in Last Character for StestTransformer. Last Character provided is ${lastChar}")
validationError = new testecord("testTransformer",
"INVALID_PARAM",
"Unexpected value in Last Character for testTransformer")}}}(output, validationError)}
Related
I have a data frame that looks like:
tdelta A B label
1 11 21 Lab1
2 24 45 Lab2
3 44 65 Lab3
4 77 22 Lab4
5 12 64 Lab5
6 39 09 Lab6
7 85 11 Lab7
8 01 45 Lab8
And I need to transform this dataset into:
For selected window: 4
A1 A2 A3 A4 B1 B2 B3 B4 L1 label
11 24 44 77 21 45 65 22 Lab1 Lab4
12 39 85 01 64 09 11 45 Lab5 Lab8
So based on the selected window - 'w', I need to transpose w rows with the first corresponding label as my X values and the corresponding last label as my Y value. here is what I have developed till now:
def data_process(data,window):
n=len(data)
A = pd.DataFrame(data['A'])
B = pd.DataFrame(data['B'])
lb = pd.DataFrame(data['lab'])
df_A = pd.concat([gsr.loc[i] for i in range(0,window)],axis=1).reset_index()
df_B = pd.concat([st.loc[i] for i in range(0,window)],axis=1).reset_index()
df_lb = pd.concat([lb.loc[0],axis=1).reset_index()
X = pd.concat([df_A,df_B,df_lab],axis=1)
Y = pd.DataFrame(data['lab']).shift(-window)
return X, Y
I think this works for only the first 'window' rows. I need it to work for my entire dataframe.
This is essentially a pivot, with a lot of cleaning up after the pivot. For the pivot to work we need to use integer and modulus division so that we can group the rows into windows of length w and figure out which column they then belong to.
# Number of rows to group together
w = 4
df['col'] = np.arange(len(df))%w + 1
df['i'] = np.arange(len(df))//w
# Reshape and flatten the MultiIndex
df = (df.drop(columns='tdelta')
.pivot(index='i', columns='col')
.rename_axis(index=None))
df.columns = [f'{x}{y}'for x,y in df.columns]
# Define these columns and remove the intermediate label columns.
df['L1'] = df['label1']
df['label'] = df[f'label{w}']
df = df.drop(columns=[f'label{i}' for i in range(1, w+1)])
print(df)
A1 A2 A3 A4 B1 B2 B3 B4 L1 label
0 11 24 44 77 21 45 65 22 Lab1 Lab4
1 12 39 85 1 64 9 11 45 Lab5 Lab8
I have a pandas dataframe that looks like:
A B C
e 34 89 90
f 92 90 40
g 45 67 80
h 76 25 98
I want to iterate over each row of a column and check if value > 80
if the condition is met, I want extract the index name and append to a list.
What's the best way to do so?
The resulting list should look like:
['f','e','f','e','g','h]
You could use stack after transposing your dataset:
df1 = df.T.stack()
df1[df 1>= 80].reset_index().level_1.values
['f' 'e' 'f' 'e' 'g' 'h']
I use Python. I created two lists with different lengths:
list_1 = [1, 2, 3, 4, 5];
list_2 = ['a', 'b', 'c'];
I want to print out pairs of numbers and letters as
"1a 2b 3c 4a 5b 1c 2a 3b 4c 5a 1b 2c 3a 4b 5c"`
to loop through both lists simultaneously.
The following nested for loop prints out pairs as
"1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c"`
which is not what I want:
for num in list_1:
for letter in list_2:
print(num, letter)
Here's a solution using zip and itertools.cycle:
>>> a = [1, 2, 3, 4, 5]
>>> b = ['a', 'b', 'c']
>>> from itertools import cycle
>>> for _, x, y in zip(range(len(a) * len(b)), cycle(a), cycle(b)):
... print(x, y)
...
1 a
2 b
3 c
4 a
5 b
1 c
2 a
3 b
4 c
5 a
1 b
2 c
3 a
4 b
5 c
Alternatively, using itertools.islice instead of range:
from itertools import cycle, islice
for x, y in islice(zip(cycle(a), cycle(b)), len(a) * len(b)):
print(x, y)
>>> a = [1, 2, 3, 4, 5]
>>> b = ['a', 'b', 'c']
>>> for x, y in zip(a * len(b), b * len(a)):
print(x, y)
1 a
2 b
3 c
4 a
5 b
1 c
2 a
3 b
4 c
5 a
1 b
2 c
3 a
4 b
5 c
My current dataframe data is as follows:
df=pd.DataFrame([[1.4,3.5,4.6],[2.8,5.4,6.4],[7.8,6.5,5.8]],columns=['t','i','m'])
t i m
0 14 35 46
1 28 54 64
2 28 34 64
3 78 65 58
My goal is to apply a vectorized operations on a df with a conditions as follows (pseudo code):
New column of answer starts with value of 1.
For row in df.itertuples():
if (m > i) & (answer in row-1 is an odd number):
answer in row = answer in row-1 + m
elif (m > i):
answer in row = answer in row-1 - m
else:
answer in row = answer in row-1
The desired output is as follows:
t i m answer
0 14 35 46 1
1 28 54 59 60
2 78 12 58 2
3 78 91 48 2
Any elegant solution would be appreciated.
I have created column as mentioned below:
0 A 00
1 B 01
2 C 02
3 D 03
4 E 04
5 F 05
6 G 06
7 H 07
8 I 08
9 J 09
10 K 010
I have two columns , first column has 0-10 and second column has A-K and I have created a new column with combinations of AA , AB till ZZ in numbers as in 00 , 01 till 010. I need to substitute the number with the alphabets using vlookup and if condition . Kindly help me with it
Can I do it with help of =Vlookup() and/or =If() condition alone?
Sorry, not really sure what you're after, I can't read your initial starting data very clearly ?
To replace a value, you can:
=SUBSTITUTE(A1,"0","A")
not sure if that does what you want, though?
I'll start a new answer, it's different enough from the first post.
So, taking your initial data:
I add 2 more columns: It's simply the mapping of Column C, split out into column D and E. At time of writing this, I have no idea how you translate that programatically, so until we have the rules, I'll just do this . and we can plug that other part in later.
0 A 00 0 0
1 B 01 0 1
2 C 02 0 2
3 D 03 0 3
4 E 04 0 4
5 F 05 0 5
6 G 06 0 6
7 H 07 0 7
8 I 08 0 8
9 J 09 0 9
10 K 010 0 10
And then use this formula:
=CONCATENATE(VLOOKUP(D1,$A$1:$B$11,2,FALSE),VLOOKUP(E1,$A$1:$B$11,2,FALSE))
and you'll end up with this:
AA
AB
AC
AD
AE
AF
AG
AH
AI
AJ
AK
If your ultimate goal is to generate AA thru ZZ, first enter the following UDF in a standard module:
Public Function BumpString(s As String) As String
Dim aryIN(1 To 1000) As Integer
Dim L As Long, i As Long, carry As Boolean
L = Len(s)
j = 1
For i = L To 1 Step -1
aryIN(j) = Asc(Mid(s, i, 1))
j = j + 1
Next i
For i = 1 To L
carry = False
aryIN(i) = aryIN(i) + 1
If aryIN(i) = 91 Then
aryIN(i) = 65
carry = True
Else
Exit For
End If
Next
If carry Then
L = L + 1
aryIN(L) = 65
End If
For i = 1 To L
BumpString = Chr(aryIN(i)) & BumpString
Next i
End Function
Then pick a cell, say D1 and enter AABelow it enter:
=bumpstring(D1)
Then copy D2 down thru D676
You don't need VLOOKUP for this.
You can use the CODE function with the first and last character:
=(CODE(LEFT(A1))-65) & (CODE(RIGHT(A1))-65)
Note that many combinations will map to the same number, such as "BU" and "MA":