Py学习  »  Python

如果python中有两个不同的符号,如何在dataframe中拆分数据?

123sqwef3 • 3 年前 • 1502 次点击  

我当前的数据框包含一个名为Gross的列。我只想要数字部分。如何将其拆分并合并回原始数据帧? 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')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133100
文章 [ 1 ]  |  最新文章 3 年前
richardec
Reply   •   1 楼
richardec    3 年前

.str.replace 我能为你做到这一点:

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

输出:

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