列中元组列表中的小写字符串

lowercase strings in a list of tuples in a column

我有这样的 fataframe:

test = {'text': [
    ('tom', 'tom', 'TOM is a good guy.'),
    ('Nick','nick', 'Is that Nick?')
]}, {'text': [
    ('juli', 'juli', 'Tom likes juli so much.'),
    ('tony', 'tony', 'Steve and Tony listen in as well.')
]}

df_test = pd.DataFrame(test)

我想将 'text' 列中的字符串小写。 有什么建议吗?

您在列中有元组列表。这是一个元组中的三个字符串。


import pandas as pd

test = {'text': [
    ('tom', 'tom', 'TOM is a good guy.'),
    ('Nick','nick', 'Is that Nick?')
]}, {'text': [
    ('juli', 'juli', 'Tom likes juli so much.'),
    ('tony', 'tony', 'Steve and Tony listen in as well.')
]}

df_test = pd.DataFrame(test)

df_test['text'].apply(lambda col: [(x[0].lower(), x[1].lower(), x[2].lower()) for x in col])

或者你可以创建一个函数来转换不同长度的字符串元组:

from typing import Tuple


def tuple_to_lower(x) -> Tuple:
    return tuple(val.lower() for val in x)

df_test['text'].apply(lambda col: [tuple_to_lower(x) for x in col])