Home > other >  I want to convert uppercase words to lowercase in dataframe and put them below
I want to convert uppercase words to lowercase in dataframe and put them below

Time:11-03

  1. convert uppercase to lowercase
  2. add below it.
en ko
Acceptive Acceptive koean mean
Access Access koean mean

=>

en ko
Acceptive Acceptive koean
acceptive Acctptive koean
Access Access koean
access Accesskoean

please help !!!!!!!

CodePudding user response:

I guess the input is like this form.

>>> df
      en  ko
0  Apple  사과
1    Ant  개미

First, repeat the rows.

>>> df2 = df.loc[df.index.repeat(2)].reset_index(drop = True)
>>> df2
      en  ko
0  Apple  사과
1  Apple  사과
2    Ant  개미
3    Ant  개미

Now, you can lower the odd rows.

>>> for row in range(len(df)):
...     df2.loc[2*row 1].en = df2.loc[2*row 1].en.lower()

>>> df2
      en  ko
0  Apple  사과
1  apple  사과
2    Ant  개미
3    ant  개미

CodePudding user response:

First, we convert each element of the en column into a list containing the same text in original and lower case. Then we use explode() to "unpack" the values of the list into different strings

df.en = df.en.apply(lambda x: [x,x.lower()])
df = df.explode('en',ignore_index=True)
print(df)

The same in one line:

df = df.assign(en=lambda y: y.en.apply(lambda x: [x, x.lower()])).explode('en',ignore_index=True)
          en                    ko
0  Acceptive  Acceptive koean mean
1  acceptive  Acceptive koean mean
2     Access     Access koean mean
3     access     Access koean mean
  • Related