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 can I split the data in dataframe if there two different signs in python?

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')

>Solution :

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

.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
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