2016-11-04 40 views
1

的字符串降熊猫据帧行我有列的熊猫数据帧包含整数

[Brand, CPL1, CPL4, Part Number, Calendar Year/Month, value, type] 

当他们出来StatsModels X13的,他们occasionaly有一个整数非常大的字符串表示的值,使在任何意义上他们的背景下,EG:

[float(1.2), float(1.3), str("63478"), float(1.1)] 

如何删除发生这种情况的行?由于它们是整数的字符串表示,我不能施放它们或任何类似的方法。

+0

什么是数据的来源是什么?缺陷列(或列中的行)的起源是什么?一些特定的样本数据和/或代码会有所帮助。 –

+0

来源是SAP Hana xls文件被导入到DataFrame中,将每个零件编号展平成一系列并从statsmodels x13出来。从x13出来的这个系列包含了这些违规行为。 –

回答

1

您可以使用boolean indexing以检查是否typestring

数据帧

df = pd.DataFrame([[float(1.2), float(1.3), str("63478"), float(1.1)], 
        [float(1.2), float(1.3), float(1.1), str("63478")]]).T 

print (df) 
     0  1 
0 1.2 1.2 
1 1.3 1.3 
2 63478 1.1 
3 1.1 63478 

print (df.applymap(lambda x: isinstance(x, str))) 
     0  1 
0 False False 
1 False False 
2 True False 
3 False True 

print (df.applymap(lambda x: isinstance(x, str)).any(axis=1)) 
0 False 
1 False 
2  True 
3  True 
dtype: bool 

print (df[~df.applymap(lambda x: isinstance(x, str)).any(axis=1)]) 
    0 1 
0 1.2 1.2 
1 1.3 1.3 

系列

s = pd.Series([float(1.2), float(1.3), str("63478"), float(1.1)]) 
print (s) 
0  1.2 
1  1.3 
2 63478 
3  1.1 
dtype: object 

print (s.apply(lambda x: isinstance(x, str))) 
0 False 
1 False 
2  True 
3 False 
dtype: bool 

print (s[~s.apply(lambda x: isinstance(x, str))]) 
0 1.2 
1 1.3 
3 1.1 
dtype: object