2017-09-16 107 views
2

任何字符串我有以下格式CSV數據刪除行:如何從大熊貓數據幀包含在特定列

+-------------+-------------+-------+ 
| Location | Num of Reps | Sales | 
+-------------+-------------+-------+ 
| 75894  |   3 | 12 | 
| Burkbank |   2 | 19 | 
| 75286  |   7 | 24 | 
| Carson City |   4 | 13 | 
| 27659  |   3 | 17 | 
+-------------+-------------+-------+ 

Location列是object數據類型。我想要做的是刪除所有具有非數字位置標籤的行。所以我的期望輸出,考慮到上面的表格是:

list1 = ['Carson City ', 'Burbank']; 
df = df[~df['Location'].isin(['list1'])] 

這是由下面的帖子的啓發:

+----------+-------------+-------+ 
| Location | Num of Reps | Sales | 
+----------+-------------+-------+ 
| 75894 |   3 | 12 | 
| 75286 |   7 | 24 | 
| 27659 |   3 | 17 | 
+----------+-------------+-------+ 

現在,我可能很難通過以下方式解決方案代碼

How to drop rows from pandas data frame that contains a particular string in a particular column?

但是,我正在尋找的是一個通用的解決方案,它將適用於上述類型的任何表。

回答

2

您可以使用pd.to_numeric要挾非數值來nan,然後過濾器的基礎上,如果位置nan

df[pd.to_numeric(df.Location, errors='coerce').notnull()] 

#Location Num of Reps Sales 
#0 75894   3  12 
#2 75286   7  24 
#4 27659   3  17 
3

或者你可以做

df[df['Location'].str.isnumeric()] 
 

    Location Num of Reps Sales 
0 75894   3  12 
2 75286   7  24 
4 27659   3  17 
1
In [139]: df[~df.Location.str.contains('\D')] 
Out[139]: 
    Location Num of Reps Sales 
0 75894   3  12 
2 75286   7  24 
4 27659   3  17 
0
df[df['Location'].str.isdigit()] 


    Location Num of Reps Sales 
0 75894   3  12 
2 75286   7  24 
4 27659   3  17