I have the following dataframe:
symbol Open close
SPY 34,2 33,2
AMZN 30.2 10,2
.................
and I want to create a function that will convert each character in a string into ASCII. This function will than be applied on symbol field and its value will be placed in a new field (called 'id'). The final dataframe will look:
symbol Open close id
SPY 34,2 33,2 838089
AMZN 30.2 10,2 65779078
this is what I have done
def symbolid(x):
strAscii = ''
for i in range (len(x)):
strAscii = strAscii str(ord(x[i]))
print(x)
return strAscii
df['id'] = df.apply(lambda x: symbolid(df['symbol']), axis=1)
I get TypeError: ord() expected a character, but string of length 3 found
any help would be appreciate/ Using python 3.6
CodePudding user response:
In your lambda function you use whole column as argument, that's source of error, you can only use symbol column.
import pandas as pd
data = {
"symbol": ["SPY","AMZN"],
"Open": [34.2 , 33.2],
"close":[30.2 ,10.2]
}
df = pd.DataFrame(data)
def symbolid(x):
strAscii = ''
for i in range (len(x)):
strAscii = strAscii str(ord(x[i]))
print(x)
return strAscii
df['id'] = df['symbol'].apply(lambda x: symbolid(x))
print(df)
CodePudding user response:
You were not that far. But in your lambda function you still refere df while you should use x:
df['id'] = df.apply(lambda x: symbolid(x['symbol']), axis=1)
shows
SPY
SPY
SPY
AMZN
AMZN
AMZN
AMZN
and gives as expected
df
symbol Open close id
0 SPY 34,2 33,2 838089
1 AMZN 30.2 10,2 65779078
But your code is not very efficient because:
- in your function you add to a string when you could join an iterator
- you convert each row into a Series when you could simply transform one column
In the end,
df['symbol'].transform(lambda x: ''.join(str(ord(i)) for i in x))
Also gives the expect dataframe
