如何在不将其转换为字符串的情况下,将 Pandas 列中的对象转换为大写?

huangapple go评论65阅读模式
英文:

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.

英文:

Use pandas.DataFrame.apply

# 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])

huangapple
  • 本文由 发表于 2023年4月13日 14:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定