Home > Blockchain >  Insert space in Pandas Data Frame Column String for each character
Insert space in Pandas Data Frame Column String for each character

Time:10-10

I have a data frame look like below I need to give space between each letter of word in same column

import pandas as pd
df = pd.DataFrame({'sequence': ['ABCAD', 'DBAACR']})
df

Expected Output

sequence
A b C A D
D B A A C R

CodePudding user response:

import pandas as pd
df = pd.DataFrame({'sequence':['ABCAD','DBAACR']})

A = []
for i in df['sequence']:
  a = (" ".join(i))
  A.append(a)

df = pd.DataFrame({'sequence':A})
df

If you execute above cell which will return the pandas DataFrame as below.

  sequence
0   A B C A D
1   D B A A C R

Thanks and don't forget to upvote :D

CodePudding user response:

pd.DataFrame({'sequence':[' '.join('ABCAD'),' '.join('DBAACR')]})

CodePudding user response:

You can use apply with lambda function to process columns in pandas data frame

df.sequence.apply(lambda x: ' '.join(list(x)))

Output:

0      A B C A D
1    D B A A C R
  • Related