Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

spliting the value by delimiter pandas

In my dataframe, I have a column called pca that has 2 values.

dataframe

I want to separate them into 2 columns.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My thinking was that, I separate them using str.split and after that remove the square bracket in each column.

I tried using
getClusterReview[['pca_A', 'pca_B']] = getClusterReview['pca'].str.split(',', expand=True)

However, I am stuck at splitting as I got an error saying column must be the same length.

>Solution :

First create the lists with the values that will be your splitted columns, and then add them to the df, and drop the old column if you want:

pca1 = []
pca2 = []
for element in getClusterReview["pca"]:
  pca1.append(element[0])
  pca2.append(element[1])

getClusterReview["pca1"] = pca1
getClusterReview["pca2"] = pca2
getClusterReview.drop("pca", axis=1)
   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading