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

How to retrieve the string after whitespace in pandas series?

I want to append string TimeClust to a space, followed by the last string (digit) of the pandas series.

time_cluster = "TimeClust" + " ".join(treatment_info.loc["Time Cluster"].to_string().split())

Series:

treatment_info.loc["Time Cluster"]

0
12    Cluster 2
48    Cluster 3
4     Cluster 1
8     Cluster 4
0     Cluster 1

Name: Time Cluster, dtype: object

Expected output:

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

    0
    12    TimeClust_2
    48    TimeClust_3
    4     TimeClust_1
    8     TimeClust_4
    0     TimeClust_1

>Solution :

IIUC, You can use Series.str.replace.

df['Time Cluster'] = df['Time Cluster'].str.replace('Cluster ', 'TimeClust_')
print(df)

    0 Time Cluster
0  12  TimeClust_2
1  48  TimeClust_3
2   4  TimeClust_1
3   8  TimeClust_4
4   0  TimeClust_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