[英]Create a python function to iterate over pandas dataframe column and convert its string value to ASCII
I have the following dataframe:我有以下 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.我想创建一个 function 将字符串中的每个字符转换为 ASCII。 This function will than be applied on symbol field and its value will be placed in a new field (called 'id').
然后,此 function 将应用于符号字段,其值将放置在新字段(称为“id”)中。 The final dataframe will look:
最终的 dataframe 将如下所示:
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我得到 TypeError: ord() expected a character, but string of length 3 found
any help would be appreciate/ Using python 3.6任何帮助将不胜感激/使用 python 3.6
You were not that far.你没那么远。 But in your lambda function you still refere
df
while you should use x
:但是在您的 lambda function 中,您仍然可以参考
df
而您应该使用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 the end,到底,
df['symbol'].transform(lambda x: ''.join(str(ord(i)) for i in x))
Also gives the expect dataframe还给出了期望 dataframe
In your lambda function you use whole column as argument, that's source of error, you can only use symbol column.在您的 lambda function 中,您使用整列作为参数,这是错误的来源,您只能使用符号列。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.