Exporting Data to CSV and Excel
Explore how to export data to CSV and Excel formats.
We'll cover the following...
Dirty Devil data
We’ll revisit the Dirty Devil data. Let’s load the flow and gauge height data:
Press + to interact
import pandas as pd # import the pandas library# define the url where the data is located and read it into a pandas dataframeurl = 'https://github.com/mattharrison/datasets/raw/master/data/dirtydevil.txt'df = pd.read_csv(url, skiprows=lambda num: num <34 or num == 35, sep='\t')# define a function to convert the timezone to America/Denverdef to_denver_time(df_, time_col, tz_col):return (df_ # the input dataframe.assign(**{tz_col: df_[tz_col].replace('MDT', 'MST7MDT')}) # replace 'MDT' with 'MST7MDT' in the timezone column.groupby(tz_col)[time_col] # group the dataframe by the timezone column and select the time column.transform(lambda s: pd.to_datetime(s) # convert the time column to datetime format.dt.tz_localize(s.name, ambiguous=True) # localize the datetime to the timezone column.dt.tz_convert('America/Denver'))) # convert the timezone to America/Denver# define a function to tweak the river datadef tweak_river(df_):return (df_ # the input dataframe.assign(datetime=to_denver_time(df_, 'datetime', 'tz_cd')) # add a new column of datetime in the America/Denver timezone.rename(columns={'144166_00060': 'cfs', '144167_00065': 'gage_height'}) # rename two columns.set_index('datetime') # set the datetime column as the index)# apply the function to tweak the river datadd = tweak_river(df)print(dd) # print the resulting dataframe
Reading and writing
There are a bunch of functions in pandas that deal with ingesting data. They all begin with read_
. Similarly, there are analagous exporting methods on the DataFrame object. These exporting methods start with to_
. We’ll talk about the common methods for exporting.
Creating CSV files
The Comma-Separated Value (CSV) file is ubiquitous. It’s been around since the early 1970s. This format has the benefit of being human-readable, and that’s about where the ...