Home > other >  How to retrieve the string after whitespace in pandas series?
How to retrieve the string after whitespace in pandas series?

Time:07-24

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:

    0
    12    TimeClust_2
    48    TimeClust_3
    4     TimeClust_1
    8     TimeClust_4
    0     TimeClust_1

CodePudding user response:

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

CodePudding user response:

In more complex cases, you can use regex lookbehind assertion (?<=\s)\w :

s = pd.Series(['foo 1', 'bar 22'])
s.str.extract(r'(?<=\s)(\w )')

Output:

    0
0   1
1  22
  • Related