Until now we have seen how to import and export data using mostly the tools provided in the Python standard library. Now, we'll see how to do some of the operations shown above in just few lines using the Pandas library. Pandas is an open source, BSD-licensed library that simplifies the process of data import and manipulation thus providing data structures and parsing functions.
We will demonstrate how to import, manipulate and export data using Pandas.
To be able to use the code in this section, we need to install Pandas.This can be done again using pip as shown here:
pip install pandas
Here, we will import again the data ch2-data.csv
, add a new column to the original data and export the result in csv, as shown in the following code snippet:
data = pd.read_csv('ch02-data.csv') data['amount_x_2'] = data['amount']*2 data.to_csv('ch02-data_more.csv)