英文:
How to capitalize a pandas object in a column without converting it into string?
问题
I want to capitalize a pandas object in a list without converting it into string for matching purpose.
这是数据集:
Column A | Column B |
---|---|
[apple pie, banana milkshake, chocolate ice cream] | [Apple Pie, Banana Milkshake, Chocolate Ice Cream] |
[chicken noodles, lamb chops, lime juice] | [Chicken Noodles, Lamb Chops, Lime Juice] |
英文:
I want to capitalize a pandas object in a list without converting it into string for matching purpose.
This is the datset:
Column A | Column B |
---|---|
[apple pie, banana milkshake, chocolate ice cream] | |
[chicken noodles, lamb chops, lime juice] |
I'm expecting column B:
Column A | Column B |
---|---|
[apple pie, banana milkshake, chocolate ice cream] | [Apple Pie, Banana Milkshake, Chocolate Ice Cream] |
[chicken noodles, lamb chops, lime juice] | [Chicken Noodles, Lamb Chops, Lime Juice] |
答案1
得分: 1
使用str.title
在列表推导中,对每个单词的首字母进行大写,因为str.capitalize
仅将第一个单词的首字母转换为大写:
df['Column B'] = [[y.title() for y in x] for x in df['Column A']]
df['Column C'] = [[y.capitalize() for y in x] for x in df['Column A']]
print(df)
Column A
0 [apple pie, banana milkshake, chocolate ice cr...
1 [chicken noodles, lamb chops, lime juice]
Column B
0 [Apple Pie, Banana Milkshake, Chocolate Ice Cr...
1 [Chicken Noodles, Lamb Chops, Lime Juice]
Column C
0 [Apple pie, Banana milkshake, Chocolate ice cr...
1 [Chicken noodles, Lamb chops, Lime juice]
英文:
Use str.title
in list comprehension for uppercase first letter for each word, because str.capitalize
convert first letter only in first word:
df['Column B'] = [[y.title() for y in x] for x in df['Column A']]
df['Column C'] = [[y.capitalize() for y in x] for x in df['Column A']]
print (df)
Column A \
0 [apple pie, banana milkshake, chocolate ice cr...
1 [chicken noodles, lamb chops, lime juice]
Column B \
0 [Apple Pie, Banana Milkshake, Chocolate Ice Cr...
1 [Chicken Noodles, Lamb Chops, Lime Juice]
Column C
0 [Apple pie, Banana milkshake, Chocolate ice cr...
1 [Chicken noodles, Lamb chops, Lime juice]
答案2
得分: 0
Sure, here's the translated code portion:
# 导入所需模块
import pandas as pd
import numpy as np
# 创建虚假数据
df = pd.DataFrame({
'Column A': [['apple pie', 'banana milkshake', 'chocolate ice cream'], ['chicken noodles', 'lamb chops', 'lime juice']],
})
# 创建列B和C
df['Column B'] = df['Column A'].apply(lambda x: [y.title() for y in x])
df['Column C'] = df['Column A'].apply(lambda x: [y.capitalize() for y in x])
You can find more information about the pandas.DataFrame.apply
function here.
英文:
# import required module
import pandas as pd
import numpy as np
# create fake data
df = pd.DataFrame({
'Column A': [['apple pie', 'banana milkshake', 'chocolate ice cream'], ['chicken noodles', 'lamb chops', 'lime juice']],
})
# create column b, c
df['Column B'] = df['Column A'].apply(lambda x: [y.title() for y in x])
df['Column C'] = df['Column A'].apply(lambda x: [y.capitalize() for y in x])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论