Python 将字符串中的所有字符更改为应用于整列的unicode
我有一个字符串:Python 将字符串中的所有字符更改为应用于整列的unicode,python,string,pandas,dataframe,unicode,Python,String,Pandas,Dataframe,Unicode,我有一个字符串: fruit1 = 'apple' 并将其更改为unicode代码点: fruit 1 = int(''.join(str(ord(char)) for char in fruit1)) print(fruit1) 97112112108101 是否可以在整个列上应用相同的概念,而不在每个值上运行for循环 Sample Table: | Fruit | ------- | apple | | berry | | kiwi | 期望输出: | Numbe
fruit1 = 'apple'
并将其更改为unicode代码点:
fruit 1 = int(''.join(str(ord(char)) for char in fruit1))
print(fruit1)
97112112108101
是否可以在整个列上应用相同的概念,而不在每个值上运行for循环
Sample Table:
| Fruit |
-------
| apple |
| berry |
| kiwi |
期望输出:
| Number |
----------------
| 97112112108101 |
| 98101114114121 |
| 107105119105 |
不幸的是,
map
和apply
是引擎盖下的循环,但在这里工作:
df['new'] = df['Fruit'].map(lambda x: int(''.join(str(ord(char)) for char in x)))
#alternative
#df['new'] = df['Fruit'].apply(lambda x: int(''.join(str(ord(char)) for char in x)))
print (df)
Fruit new
0 apple 97112112108101
1 berry 98101114114121
2 kiwi 107105119105
除了您需要的原因(?!),是的,这是可能的:
df['Fruit'] = df['Fruit'].apply(lambda fruit1: int(''.join(str(ord(char)) for char in fruit1)))