Home > Software engineering >  lowercase strings in a list of tuples in a column
lowercase strings in a list of tuples in a column

Time:09-28

I have a fataframe like this:

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)

I want to lowercase the strings in the column 'text'. Any suggestions?

CodePudding user response:

You have list of tuples insde the column. This is for three string in one tuple.


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

Or you can create a function to transform tuple of string with different length:

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])
  • Related