2

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?

Alina
  • 61
  • 6
  • Does this answer your question? [How to lowercase a pandas dataframe string column if it has missing values?](https://stackoverflow.com/questions/22245171/how-to-lowercase-a-pandas-dataframe-string-column-if-it-has-missing-values) – rm -f me Sep 27 '21 at 16:18
  • that doesn't look like works for me - text is a list here – rv.kvetch Sep 27 '21 at 16:21

1 Answers1

2

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])
Peter Trcka
  • 1,279
  • 1
  • 16
  • 21