i'm trying to do some date cleaning on some huge datasets, and I'm new to python (I've used google to search for my problem), so please bear over me with my terminology.
The data is imported from a CSV into a pandas.core.frame.DataFrame Some of my columns should only contain numbers and others only text:
CPRNUM REQ_SAMPLETIME SAMPLE_ID RESULT
0 1234567890 2014-05-30 07:59 50226686 0.7409090909090907
1 The sample was.. 2013-09-04 07:45 47721186 0.8290909090909093
2 1234567890 The sample was.. 46473016 1.0918181818181818
I would really like to get rid of the rows within column REQ_SAMPLETIME and CPRNUM which is not 10 digits long, and contain text, so it would look like:
CPRNUM REQ_SAMPLETIME SAMPLE_ID RESULT
0 1234567890 2014-05-30 07:59 50226686 0.7409090909090907
3 0987654321 2018-06-10 05:32 12354678 3.7290909090909093
4 1234567890 2013-09-04 07:45 15672687 5.9999951818181818
Thanks for the help
Thanks to Danish Bansal, I used your code, as it fits my problem the most:
My final code looks like this:
hba1c = pd.read_csv(r"C:\sample.csv", encoding = 'unicode_escape', engine ='python', sep = ';')
#This function checks if the number is valid
def isCPRNUMvalid(val):
if len(val) == 10: #if string has length of 10
if val.isnumeric(): #if string is pure number
return True
return False
hba1c['validCRPNUM'] = hba1c['CPRNUM'].apply(isCPRNUMvalid)
hba1c = hba1c[hba1c.validCRPNUM]
hba1c['Dates'] = pd.to_datetime(hba1c['REQ_SAMPLETIME']).dt.date
C:\Users\tphni\.conda\envs\py37\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""Entry point for launching an IPython kernel.
hba1c.head()
I get the error message as typed above in the code, but it still seems to work, and the output is as excepted, with the correct number of fewer rows than the original file.