使用Python删除包含数字和字符串的数据框中的小数点

时间:2014-04-10 09:12:28

标签: python pandas dataframe

我有一个包含大约50,000条记录的数据框;我注意到列中所有数字后面都添加了“.0”。我一直试图删除“.0”,以便下表;

N  | Movies              
1  | Save the Last Dance 
2  | Love and Other Drugs
3  | Dance with Me      
4  | Love Actually       
5  | High School Musical
6  | 2012.0      <-----
7  | Iron Man     
8  | 300.0       <-----
9  | Inception      
10 | 360.0       <-----
11 | Pulp Fiction

看起来像这样;

N  | Movies              
1  | Save the Last Dance 
2  | Love and Other Drugs
3  | Dance with Me      
4  | Love Actually       
5  | High School Musical
6  | 2012     <-----
7  | Iron Man     
8  | 300      <-----
9  | Inception      
10 | 360      <----- 
11 | Pulp Fiction

挑战在于该列包含数字和字符串。

这是可能的,如果是的话,怎么样?

提前致谢。

3 个答案:

答案 0 :(得分:4)

使用函数并应用于整列:

In [94]:

df = pd.DataFrame({'Movies':['Save the last dance', '2012.0']})
df
Out[94]:
                Movies
0  Save the last dance
1               2012.0

[2 rows x 1 columns]

In [95]:

def trim_fraction(text):
    if '.0' in text:
        return text[:text.rfind('.0')]
    return text

df.Movies = df.Movies.apply(trim_fraction)

In [96]:

df
Out[96]:
                Movies
0  Save the last dance
1                 2012

[2 rows x 1 columns]

答案 1 :(得分:0)

这是给你的提示,

如果是有效号码,

a="2012.0"
try:
    a=float(a)
    a=int(a)
    print a

except:
    print a

输出:

2012

如果像“与我共舞”这样的字符串

a="Dance with Me"
try:
    a=float(a)
    a=int(a)
    print a

except:
    print a

输出:

Dance with Me

答案 2 :(得分:0)

Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> str1 = "300.0"
>>> str(int(float(str1)))
'300'
>>>