Converting strings in rows of a Pandas Dataframe into characters separated by commas

I am dealing with a dataframe that has sequence data in each row, formatted like this.

MKEYGEDLK

My goal is to process the sequence strings in each row to have them presented in this format:

[M, K, E, Y, G, E, D, L, K]

I attempted the following code:

get_seq_str = ','.join(test_df.loc[0]['seq_1'])
arr.append(get_seq_str)

However, when I add it to the dataframe, there are single quotation marks at the beginning and end of each string which I want to remove.

[M, K, E, Y, G, E, D, L, K]

Is there a way to strip these single quotation marks?

Answer №1

As I understand it, one approach you can take is using the apply method with the list function on a string value.

df['col_list'] = df['col'].apply(list)
print(df)

         col                     col_list
0  MKEYGEDLK  [M, K, E, Y, G, E, D, L, K]

Answer №2

To extract individual characters from a string in a DataFrame column, you can utilize the str.findall method:

df['new_column'] = df['original_column'].str.findall(r'[a-zA-Z]')

For example:

         original_column              new_column
0    MKEYGEDLK             [M, K, E, Y, G, E, D, L, K]
1  ?MKEY GEDLK           [M, K, E, Y, G, E, D, L, K]

Answer №3

Here is one way to achieve the desired result:

sequence_string = [*sample_data_frame.iloc[0]['sequence_1']]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there compatibility for Python 3 in nolearn/lasagne?

While delving into Neural Net implementation using nolearn.lasagne as detailed in this resource, I encountered an issue: ImportError: No module named 'cPickle' After some investigation, I realized that cPickle is referred to as pickle in Pyth ...

Experimenting with FastAPI's TestClient results in a 422 response code for specific

Hey there, I'm currently facing an issue while testing my code. I am working with FastAPI and pydantic's BaseModel. # Model class Cat(BaseModel): breed: str location_of_origin: str coat_length: int body_type: str pattern: str ...

What could be causing my web scraper to not extract the necessary data?

I recently created a Python web scraper using Selenium that successfully opens the desired URL, although it only scrapes one page instead of all pages. However, I am encountering an issue where there is no output generated after running the code and the CS ...

I have a pair of pyspark dataframes and I am looking to compute the total sum of points in the second dataframe, which will be based on the

Welcome to my first data frame that showcases player points Playername pid matchid points 0 Virat Kohli 10 2 0 1 Ravichandran Ashwin 11 2 9 2 Gautam Gambhir 12 2 1 3 Ravindra Jadeja 13 2 7 4 Amit Mishra 14 2 2 5 Mohammed Shami 15 2 2 6 ...

Linking a function to multiple labels with the same name - tkinter

I need to create a feature that allows users to generate multiple labels by pressing the "Enter" key: def new_line(event): global Row_n Row_n = Row_n + 1 Choose= tk.Label(frame, text="Choose option", background = "white",fo ...

Tips for preserving a collection of items in a thesaurus?

My project involves a Python 3.5 program that manages an inventory of various objects. One key aspect is the creation of a class called Trampoline, each with specific attributes such as color, size, and spring type. I frequently create new instances of thi ...

A guide on combining elements within a list of lists

Is there a way to combine lists that share the same value at index[0] [['Vienna', 3, 42, 0], ['London', 4, 11, 1], ['Vienna', 1, 8, 2]] Although my question remains unanswered, I will rephrase it. How can I merge multiple lis ...

Python code to find all combinations of pairs for a given function's values

I have multiple sets of coordinates and I am looking to calculate the distance between each pair. Despite being able to compute the distances, I am struggling to display the corresponding coordinate pairs in my program. import itertools import math point1 ...

Automate page scrolling using Python and Selenium

Here's the issue at hand: I am currently using selenium to extract all successful projects from a specific webpage (""). Despite my efforts, the URL remains constant even after clicking on different buttons. I am particularly interested in successful ...

Transferring cookie data between requests in CrawlSpider

My current project involves scraping a bridge website to gather data from recent tournaments. I have previously asked for help on this issue here. Thanks to assistance from @alecxe, the scraper now successfully logs in while rendering JavaScript with Phant ...

Python code for arranging the output in numerical sequence

My program reads the last line of multiple files simultaneously and displays the output as a list of tuples. from os import listdir from os.path import isfile, join import subprocess path = "/home/abc/xyz/200/coord_b/" filename_last_l ...

The saving of data in the session is not working as expected

Currently, I am in the process of building a web application with Google App Engine and Python. I have encountered an unusual issue that has left me stumped on how to resolve it and what might be causing it. The problem arises when I fill out a form and se ...

Locate a Sub-Child href Element using Selenium

I am attempting to interact with a link using Selenium automation tool. <div id="RECORD_2" class="search-results-item"> <a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurl ...

How can I implement a waiting mechanism for the loading g-loading-icon on Google's People Also Ask feature using Selenium and Python?

First and foremost, I want to express my gratitude to @cruisepandey for assisting me with the following topic: How can one crawl question and answer content from Google's "People Also Ask" feature using Selenium and Python? Following his guidance, I ...

Sending a post request with a PIL Image in Python

<PIL.WebPImagePlugin.WebPImageFile image mode=RGB size=1600x1600 at 0x1F4E779BA00> is the type of file I'm working with. When I make a post request, I encounter this error: TypeError: a bytes-like object is required, not 'WebPImageFile&apos ...

Creating a matrix or table in Python to analyze overlapping data frames and count the intersections

Python seems to hold the key to a problem I encountered recently. dataframe 1 dataframe 2 dataframe 3 SID UID SID UID SID UID 123 dog 456 dog 789 monkey 123 cat 456 bat 789 ...

Python code: Transforming points on a contour into a bounding box

Currently, I am analyzing medical images using XML files that contain contour coordinates detailing regions of interest. Despite successfully extracting these points, I am facing challenges in converting them into a bounding box suitable for creating masks ...

Unlocking the secrets of password-protected website scraping

Recently, I've been working on a project that involves scraping data from a website (specifically to extract saved words). The catch is, this website has password protection and utilizes some JavaScript functionalities that puzzle me (certain elemen ...

Using Selenium and Python to showcase the source of an image within an iframe

My goal is to automatically download an image from shapeNet using Python and selenium. I have made progress, but I am stuck on the final step. from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.s ...

Saving a Python pandas dataframe to a CSV file with line breaks represented as text

How can I write the sentence "hello \n world\n." in a cell without "\n" being interpreted as end of line, so that when opened in a text editor it appears exactly as "hello \n world\n."? ...