在python 3.4.1中删除具有字符串值的熊猫数据框的行

sayak_ghosh90

我已经读取了包含read_csv8列熊猫的csv文件每列可能包含int / string / float值。但是我想删除那些具有字符串值的行,并返回其中仅包含数字值的数据框。附加csv示例。
我尝试运行以下代码:

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
埃德·楚姆

因此,我要采用的方法是尝试使用带Try/的用户函数将列转换为int,Catch以处理无法将值强制转换为Int的情况,这些将被设置为NaNvalue。将行放在一个空值处,由于某种原因,当我用您的数据对其进行测试时,它的长度实际上为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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ANTLR4 Python3从字符串中删除引号

来自分类Dev

Python 数据框:创建有条件地连接来自 1 或 3 个其他列的字符串值的新列

来自分类Dev

如何创建具有某些值的自定义数据框作为熊猫中的列(Python3)?

来自分类Dev

Python Pandas:如果字符串值列表== [无],则从数据框中删除行

来自分类Dev

无法在Python 3中打印列表(range(4 ** 4 ** 4))

来自分类Dev

如何在Python数据框中添加具有字典的字符串值的列

来自分类Dev

python:删除pandas数据框中包含字符串的所有行

来自分类Dev

python:删除pandas数据框中包含字符串的所有行

来自分类Dev

从python 3中的字符串中删除某些字符

来自分类Dev

我们如何从python 3的字符串中删除所有表情符号值?

来自分类Dev

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

来自分类Dev

对具有字母数字值的字符串数组进行排序(Python3)

来自分类Dev

确定字符串在Python中是否具有3个或更多重复的连续字符

来自分类Dev

尝试从Python 3中的字符串中删除字母

来自分类Dev

在Python3中从base64编码的字符串中删除新行“ \ n”?

来自分类Dev

python3-qt4从QApplication构造函数中的Unicode字符串中丢弃非ASCII字符

来自分类Dev

python中[:,3]和[:,3:4]之间的区别?

来自分类Dev

Python Pandas:使用公共列将具有字符串、数字和 NaN 值的 3 个数据帧分组到新数据帧

来自分类Dev

在小型程序中测试字符串不具有特定特征(Python 3)

来自分类Dev

Python 3 将 IPV4Address 转换为字符串

来自分类Dev

如何删除 Python 数据框单元格内的字符串中的重复值

来自分类Dev

ValueError:传递了4列,将python列表转换为数据框时,传递的数据有3列。如果3通过,如何添加空白值?

来自分类Dev

如何在字符串中删除1个x字符实例并找到它在Python3中构成的单词?

来自分类Dev

Python 3:用有限的代码行替换列表中的字符串

来自分类Dev

iTunes MP4标签/具有Python3和Mutagen的元数据

来自分类Dev

在Python 3中,如何从字符串中删除所有非UTF8字符?

来自分类Dev

如何找出 Python3 中的字符串中有多少个“1”包?

来自分类Dev

从python列表中删除具有某些值的字符串

来自分类Dev

在 Python 3 中使用 Pandas,如何过滤掉数据框中列中的重复字符串?

Related 相关文章

  1. 1

    ANTLR4 Python3从字符串中删除引号

  2. 2

    Python 数据框:创建有条件地连接来自 1 或 3 个其他列的字符串值的新列

  3. 3

    如何创建具有某些值的自定义数据框作为熊猫中的列(Python3)?

  4. 4

    Python Pandas:如果字符串值列表== [无],则从数据框中删除行

  5. 5

    无法在Python 3中打印列表(range(4 ** 4 ** 4))

  6. 6

    如何在Python数据框中添加具有字典的字符串值的列

  7. 7

    python:删除pandas数据框中包含字符串的所有行

  8. 8

    python:删除pandas数据框中包含字符串的所有行

  9. 9

    从python 3中的字符串中删除某些字符

  10. 10

    我们如何从python 3的字符串中删除所有表情符号值?

  11. 11

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

  12. 12

    对具有字母数字值的字符串数组进行排序(Python3)

  13. 13

    确定字符串在Python中是否具有3个或更多重复的连续字符

  14. 14

    尝试从Python 3中的字符串中删除字母

  15. 15

    在Python3中从base64编码的字符串中删除新行“ \ n”?

  16. 16

    python3-qt4从QApplication构造函数中的Unicode字符串中丢弃非ASCII字符

  17. 17

    python中[:,3]和[:,3:4]之间的区别?

  18. 18

    Python Pandas:使用公共列将具有字符串、数字和 NaN 值的 3 个数据帧分组到新数据帧

  19. 19

    在小型程序中测试字符串不具有特定特征(Python 3)

  20. 20

    Python 3 将 IPV4Address 转换为字符串

  21. 21

    如何删除 Python 数据框单元格内的字符串中的重复值

  22. 22

    ValueError:传递了4列,将python列表转换为数据框时,传递的数据有3列。如果3通过,如何添加空白值?

  23. 23

    如何在字符串中删除1个x字符实例并找到它在Python3中构成的单词?

  24. 24

    Python 3:用有限的代码行替换列表中的字符串

  25. 25

    iTunes MP4标签/具有Python3和Mutagen的元数据

  26. 26

    在Python 3中,如何从字符串中删除所有非UTF8字符?

  27. 27

    如何找出 Python3 中的字符串中有多少个“1”包?

  28. 28

    从python列表中删除具有某些值的字符串

  29. 29

    在 Python 3 中使用 Pandas,如何过滤掉数据框中列中的重复字符串?

热门标签

归档