Removing dataframe rows containing a certain type

dayum

I have a dataframe like this:

>>> d
Out[28]: 
                         A                     B      C      D       E
2017-06-08 20:39:00 1260.00  1903-08-12 00:00:00 230.00 245.00 19954.55
2017-06-08 20:40:00 1260.00                 1330 230.00 245.00 19966.51
2017-06-08 20:48:00 1260.00                 1320 230.00 240.00 19961.00
2017-06-08 21:02:00 1240.00                 1330 230.00 245.00 19951.38
2017-06-08 21:06:00 1240.00                 1340   5.00 240.00 19966.84
2017-06-08 21:07:00 1240.00                 1350 220.00 230.00 20000.24
2017-06-08 21:08:00 1250.00                 1370 220.00 230.00 20004.66
2017-06-11 20:31:00 1220.00                 1280 235.00 245.00 19913.86

I want to remove all the values (except in column A) that are of type datetime.datetime (here the first one in column B). I tried the following but didn't work (intention was to convert datetime to nan and remove nan values later):

d[type(d)==pd.datetime]=np.nan

I also tried this with each individual columns , i.e. the following:

df=d['B'].copy()
df[type(df)==pd.datetime]=np.nan
cs95

A simple boolean indexing is not sufficient. You'll need to check the datetime for each item.

Input:

In [239]: df
Out[239]: 
                  Col1                 Col2
0  1903-08-12 00:00:00                    1
1                    1                  abc
2                    2                    2
3                 1234                 1234
4                  abc  1903-08-12 00:00:00

Option 1

Using df.apply and pd.to_datetime, followed by df.isnull and boolean indexing. Use df.dropna to drop rows with NaN.

In [290]: df[df.apply(pd.to_datetime, errors='coerce').isnull()].dropna()
Out[290]: 
   Col1  Col2
1     1   abc
2     2     2
3  1234  1234

Option 2

A direct application of pd.datetime (not using df.apply):

In [57]: df[pd.to_datetime(df.stack(), 'coerce').unstack().isnull()].dropna()
Out[57]: 
   Col1  Col2
1     1   abc
2     2     2
3  1234  1234

Option 3

Using df.mask (thank you piRSquared!)

In [62]: df.mask(pd.to_datetime(df.stack(), 'coerce').notnull().unstack()).dropna()
Out[62]: 
   Col1  Col2
1     1   abc
2     2     2
3  1234  1234

Option 4

You can use df.applymap

In [240]: df[~df.applymap(lambda x: isinstance(x, pd.datetime))].dropna()
Out[240]: 
   Col1  Col2
1     1   abc
2     2     2
3  1234  1234

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Retriving certain amount of rows from dataframe

From Dev

Filtering a Pandas DataFrame Without Removing Rows

From Dev

Selecting rows with a certain weekday in DataFrame in Python

From Dev

DataFrame.drop_duplicates and DataFrame.drop not removing rows

From Dev

Select rows from pandas DataFrame of a certain date

From Dev

Removing lines from a string containing certain character

From Dev

Removing Duplicate rows and Calculate the Average in a Dataframe in R

From Dev

Remove certain rows from dataframe based on index

From Dev

How is geom_point removing rows containing missing values?

From Dev

removing duplicate rows in pandas DataFrame based on a condition

From Dev

python pandas dataframe : removing selected rows

From Dev

How to plot certain rows of a pandas dataframe?

From Dev

removing data frame rows based on separate dataframe

From Dev

summing up certain rows in a panda dataframe

From Dev

Pandas Dataframe: Removing redundant rows in headers

From Dev

Select rows containing certain values from pandas dataframe

From Dev

Removing rows based on column in another dataframe

From Dev

Removing certain rows in Pandas Dataframe by string format

From Dev

removing rows with any column containing NaN, NaTs, and nans

From Dev

Removing rows containing NA in every column

From Dev

Promote certain rows to index in an ordered pandas dataframe

From Dev

How to hide table rows containing certain keywords?

From Dev

Removing rows based on certain criteria in a column number

From Dev

Removing rows from a dataframe by identifying rows to keep

From Dev

Removing certain rows with a condition

From Dev

Removing rows of dataframe based on frequency of a variable

From Dev

filtering, averaging and removing rows in pandas dataframe

From Dev

delete rows in dataframe under certain value

From Dev

Filter out all rows in a dataframe containing '**'

Related Related

  1. 1

    Retriving certain amount of rows from dataframe

  2. 2

    Filtering a Pandas DataFrame Without Removing Rows

  3. 3

    Selecting rows with a certain weekday in DataFrame in Python

  4. 4

    DataFrame.drop_duplicates and DataFrame.drop not removing rows

  5. 5

    Select rows from pandas DataFrame of a certain date

  6. 6

    Removing lines from a string containing certain character

  7. 7

    Removing Duplicate rows and Calculate the Average in a Dataframe in R

  8. 8

    Remove certain rows from dataframe based on index

  9. 9

    How is geom_point removing rows containing missing values?

  10. 10

    removing duplicate rows in pandas DataFrame based on a condition

  11. 11

    python pandas dataframe : removing selected rows

  12. 12

    How to plot certain rows of a pandas dataframe?

  13. 13

    removing data frame rows based on separate dataframe

  14. 14

    summing up certain rows in a panda dataframe

  15. 15

    Pandas Dataframe: Removing redundant rows in headers

  16. 16

    Select rows containing certain values from pandas dataframe

  17. 17

    Removing rows based on column in another dataframe

  18. 18

    Removing certain rows in Pandas Dataframe by string format

  19. 19

    removing rows with any column containing NaN, NaTs, and nans

  20. 20

    Removing rows containing NA in every column

  21. 21

    Promote certain rows to index in an ordered pandas dataframe

  22. 22

    How to hide table rows containing certain keywords?

  23. 23

    Removing rows based on certain criteria in a column number

  24. 24

    Removing rows from a dataframe by identifying rows to keep

  25. 25

    Removing certain rows with a condition

  26. 26

    Removing rows of dataframe based on frequency of a variable

  27. 27

    filtering, averaging and removing rows in pandas dataframe

  28. 28

    delete rows in dataframe under certain value

  29. 29

    Filter out all rows in a dataframe containing '**'

HotTag

Archive