删除包含整数字符串的 Pandas DataFrame 行

Drop Pandas DataFrame rows that contain string of interger

我有一个 pandas 包含列的 DataFrame

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

当他们从 StatsModels X13 出来时,他们偶尔会在值中使用非常大的整数字符串表示形式,这些值在他们的上下文中没有意义,例如:

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

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

您可以使用 boolean indexing with checking if type is string:

DataFrame:

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