Home > Back-end >  How can I split the data in dataframe if there two different signs in python?
How can I split the data in dataframe if there two different signs in python?

Time:04-28

My current data frame contains a column called Gross. I only want the numbers part. How can I split it and merge it back to the original data frame? Current data frame

moive_df=pd.DataFrame({
    'Movie Names':names,
    'Rating':ratings,
    'Metascore':meta,
    'Gross':Gross,
    'Vote':votes,
    'Genre':genres})
moive_df.set_index('Movie Names')

CodePudding user response:

.str.replace can do that for you:

df['Gross'] = df['Gross'].str.replace('^[^\d] |[^\d] $', '', regex=True).astype('float')

Output:

>>> df['Gross']
0     0.43  
1     142.50
2     858.37
3     335.45
4     316.83
5     165.36
6     27.33 
7     27.33 
8     426.83
9     53.37 
10    0.35  
11    0.35  
12    108.10
13    515.20
14    390.53
15    390.53
16    543.64
17    159.23
18    159.23
19    159.23
20    12.14 
21    117.62
22    7.74  
23    7.74  
24    56.85 
25    7.00  
26    7.00  
27    96.85 
28    0.40  
Name: Gross, dtype: float64
  • Related