Python Forum
drop rows that doesnt matched
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drop rows that doesnt matched
#5
(Oct-26-2019, 04:51 PM)snippsat Wrote:
(Oct-26-2019, 02:14 PM)karlito Wrote: I want to drop rows that doesnt start/look like a date
Ok,is better if you post a sample of csv file and not image.
Can not use data from a image to test with.
>>> import pandas as pd

>>> df = pd.DataFrame({'date': ['12.06.2017', '2003', '114999999', '20.06.2017', '2.08.2018', '554777777'],'value': range(6)})
>>> df
         date  value
0  12.06.2017      0
1        2003      1
2   114999999      2
3  20.06.2017      3
4   2.08.2018      4
5   554777777      5

# Make NaT of row to be dropped
>>> pd.to_datetime(df['date'], format='%d.%m.%Y', errors='coerce')
0   2017-06-12
1          NaT
2          NaT
3   2017-06-20
4   2018-08-02
5          NaT
Name: date, dtype: datetime64[ns]

# Apply
>>> df['date'] = pd.to_datetime(df['date'], format='%d.%m.%Y', errors='coerce')
>>> df = df.dropna()
>>> df
        date  value
0 2017-06-12      0
3 2017-06-20      3
4 2018-08-02      4

# Turn date around to original format if that's needed
>>> df['date'] = df['date'].dt.strftime('%d.%m.%Y')
>>> df
         date  value
0  12.06.2017      0
3  20.06.2017      3
4  02.08.2018      4

Hi Snippsat,

Thks for your help but I tried it and it doesn't work
link : error

import pandas as pd
 
df = pd.DataFrame({0: ['09.05.2017 13:56', '1494331179', '1494331625', '09.05.2017 14:11', '944006550', '03.07.2017 16:50'],1: range(6)})
df

	              0	    1
0	09.05.2017 13:56	0
1	1494331179	        1
2	1494331625	        2
3	09.05.2017 14:11	3
4	944006550	        4
5	03.07.2017 16:50	5
Reply


Messages In This Thread
drop rows that doesnt matched - by karlito - Oct-25-2019, 05:58 PM
RE: drop rows that doesnt matched - by snippsat - Oct-26-2019, 11:57 AM
RE: drop rows that doesnt matched - by karlito - Oct-26-2019, 02:14 PM
RE: drop rows that doesnt matched - by snippsat - Oct-26-2019, 04:51 PM
RE: drop rows that doesnt matched - by karlito - Oct-28-2019, 10:19 AM
RE: drop rows that doesnt matched - by snippsat - Oct-28-2019, 10:55 AM
RE: drop rows that doesnt matched - by karlito - Oct-28-2019, 12:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Drop rows if a set of columns has a value dervast 1 2,033 Sep-12-2019, 04:18 PM
Last Post: sd_0912
  Drop rows from data with zero value Devilish 3 3,804 Dec-27-2018, 02:06 AM
Last Post: Devilish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020