2014-10-27 61 views
1

我读过一个csv文件,其中包含8列的熊猫read_csv。每列可能包含int/string/float值。但是我想删除那些具有字符串值的行,并返回一个只有数字值的数据框。附加csv示例。
我试图运行此下面的代码:在Python中删除具有字符串值的熊猫数据框的行3.4.1

import pandas as pd 
import numpy as np 
df = pd.read_csv('new200_with_errors.csv',dtype={'Geo_Level_1' : int,'Geo_Level_2' : int,'Geo_Level_3' : int,'Product_Level_1' : int,'Product_Level_2' : int,'Product_Level_3' : int,'Total_Sale' : float}) 
print(df) 

,但我得到了以下错误:

TypeError: unorderable types: NoneType() > int() 

我与Python 3.4.1运行。 这是示例csv。

Geo_L_1,Geo_L_2,Geo_L_3,Pro_L_1,Pro_L_2,Pro_L_3,Date,Sale 
1, 2, 3, 129, 1, 5193316745, 1/1/2012, 9 
1 ,2, 3, 129, 1, 5193316745, 1/1/2013, 
1, 2, 3, 129, 1, 5193316745, , 8 
1, 2, 3, 129, NA, 5193316745, 1/10/2012, 10 
1, 2, 3, 129, 1, 5193316745, 1/10/2013, 4 
1, 2, 3, ghj, 1, 5193316745, 1/10/2014, 6 
1, 2, 3, 129, 1, 5193316745, 1/11/2012, 4 
1, 2, 3, 129, 1, ghgj, 1/11/2013, 2 
1, 2, 3, 129, 1, 5193316745, 1/11/2014, 6 
1, 2, 3, 129, 1, 5193316745, 1/12/2012, ghgj 
1, 2, 3, 129, 1, 5193316745, 1/12/2013, 5 
+0

我只计算5列。 Geo_Level_1..3在哪里? – fredtantini 2014-10-27 08:05:34

+0

您必须发布完整df的原始数据,您必须在读取到熊猫之前或之后清理csv – EdChum 2014-10-27 08:17:44

+0

样本数据在上述列中存在这些错误,这就是为什么我只给出样本数据由所有8列组成。 @fredtantini – 2014-10-27 08:50:00

回答

0

因此,我想接近这个办法是尝试将列转换为int使用用户功能与Try/Catch来处理其中的值不能被强制转换为一个int的情况下,这些都将置到NaN的值。掉落,你有一个空值的行,由于某种原因,它实际上有1时,我测试了这个与你的数据的长度,它可能工作您使用LEN 0

In [42]: 
# simple function to try to convert the type, returns NaN if the value cannot be coerced 
def func(x): 
    try: 
     return int(x) 
    except ValueError: 
     return NaN 
# assign multiple columns 
df['Pro_L_1'], df['Pro_L_3'], df['Sale'] = df['Pro_L_1'].apply(func), df['Pro_L_3'].apply(func), df['Sale'].apply(func) 
# drop the 'empty' date row, take a copy() so we don't get a warning 
df = df.loc[df['Date'].str.len() > 1].copy() 
# convert the string to a datetime, if we didn't drop the row it would set the empty row to today's date 
df['Date']= pd.to_datetime(df['Date']) 
# now convert all the dtypes that are numeric to a numeric dtype 
df = df.convert_objects(convert_numeric=True) 
# check the dtypes 
df.dtypes 

Out[42]: 
Geo_L_1    int64 
Geo_L_2    int64 
Geo_L_3    int64 
Pro_L_1   float64 
Pro_L_2   float64 
Pro_L_3   float64 
Date  datetime64[ns] 
Sale    float64 
dtype: object 
In [43]: 
# display the current situation 
df 
Out[43]: 
    Geo_L_1 Geo_L_2 Geo_L_3 Pro_L_1 Pro_L_2  Pro_L_3  Date Sale 
0   1  2  3  129  1 5193316745 2012-01-01  9 
1   1  2  3  129  1 5193316745 2013-01-01 NaN 
3   1  2  3  129  NaN 5193316745 2012-01-10 10 
4   1  2  3  129  1 5193316745 2013-01-10  4 
5   1  2  3  NaN  1 5193316745 2014-01-10  6 
6   1  2  3  129  1 5193316745 2012-01-11  4 
7   1  2  3  129  1   NaN 2013-01-11  2 
8   1  2  3  129  1 5193316745 2014-01-11  6 
9   1  2  3  129  1 5193316745 2012-01-12 NaN 
10  1  2  3  129  1 5193316745 2013-01-12  5 
In [44]: 
# drop the rows 
df.dropna() 
Out[44]: 
    Geo_L_1 Geo_L_2 Geo_L_3 Pro_L_1 Pro_L_2  Pro_L_3  Date Sale 
0   1  2  3  129  1 5193316745 2012-01-01  9 
4   1  2  3  129  1 5193316745 2013-01-10  4 
6   1  2  3  129  1 5193316745 2012-01-11  4 
8   1  2  3  129  1 5193316745 2014-01-11  6 
10  1  2  3  129  1 5193316745 2013-01-12  5 

最后一行分配它所以df = df.dropna()

+0

这是伟大的.. Thanx很多..这对我有用。 @Edchum – 2014-10-27 11:20:07

相关问题