Pandas
keyboard_arrow_down 655 guides
chevron_leftCreating DataFrames Cookbook
Combining multiple Series into a DataFrameCombining multiple Series to form a DataFrameConverting a Series to a DataFrameConverting list of lists into DataFrameConverting list to DataFrameConverting percent string into a numeric for read_csvConverting scikit-learn dataset to Pandas DataFrameConverting string data into a DataFrameCreating a DataFrame from a stringCreating a DataFrame using listsCreating a DataFrame with different type for each columnCreating a DataFrame with empty valuesCreating a DataFrame with missing valuesCreating a DataFrame with random numbersCreating a DataFrame with zerosCreating a MultiIndex DataFrameCreating a Pandas DataFrameCreating a single DataFrame from multiple filesCreating empty DataFrame with only column labelsFilling missing values when using read_csvImporting DatasetImporting tables from PostgreSQL as Pandas DataFramesInitialising a DataFrame using a constantInitialising a DataFrame using a dictionaryInitialising a DataFrame using a list of dictionariesInserting lists into a DataFrame cellKeeping leading zeroes when using read_csvParsing dates when using read_csvPreventing strings from getting parsed as NaN for read_csvReading data from GitHubReading file without headerReading large CSV files in chunksReading n random lines using read_csvReading space-delimited filesReading specific columns from fileReading tab-delimited filesReading the first few lines of a file to create DataFrameReading the last n lines of a fileReading URL using read_csvReading zipped csv file as a DataFrameRemoving Unnamed:0 columnResolving ParserError: Error tokenizing dataSaving DataFrame as zipped csvSkipping rows without skipping header for read_csvSpecifying data type for read_csvTreating missing values as empty strings rather than NaN for read_csv
check_circle
Mark as learned thumb_up
4
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Parsing dates when using read_csv in Pandas
schedule Aug 12, 2023
Last updated local_offer
Tags Python●Pandas
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
Parsing columns as datetime
Consider the following my_data.txt
file:
A,B2020/12/25,72020/12,82020,9
To parse column A
as a datetime when using read_csv(~)
:
Parsing index as datetime
Consider the following my_data.txt
file:
A2020/12/25,72020/12,82020,9
To parse the index as datetime:
df
A2020-12-25 72020-12-01 82020-01-01 9
Here, the index is of type DatetimeIndex
:
df.index
DatetimeIndex(['2020-12-25', '2020-12-01', '2020-01-01'], dtype='datetime64[ns]', freq=None)
Combining multiple columns to form a single datetime column
Consider the following my_data.txt
file:
Year,Month2020,72020,82020,9
Using a nested list
To combine columns Year
and Month
to form a single datetime column:
df
Year_Month0 2020-07-011 2020-08-012 2020-09-01
To confirm its data type:
df.dtypes
Year_Month datetime64[ns]dtype: object
Using a dictionary
To combine columns Year
and Month
to form a single datetime column:
df
A0 2020-07-011 2020-08-012 2020-09-01
Using a dictionary is more flexible than using a nested list because:
you can specify a label to the combined column (e.g.
"A"
in this case)you can specify multiple groups of columns to combine as a single date column.
Related
Pandas | read_csv method
Reads a file, and parses its content into a DataFrame.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
4
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!