Writing Data

to_csv

pandas can also write data to a variety of file formats, including CSV, Excel, and SQL databases. The following code cell writes the elections dataset to a CSV file named elections.csv.



To write a DataFrame to a CSV file, use the df.to_csv() function. The first input to df.to_csv() is the filename or filepath that you want to write to.

pd.to_csv('elections_new.csv')

Other important parameters of the df.to_csv() function are:

  1. sep: (default: sep=',') specifies the separator used to separate columns. Default is , which means the columns are separated by a comma.

  2. header: (default: header=True) specifies whether to write the header row. Default is True which means the header row is written. If you don’t want to write the header row, then header=False should be used.

  3. index: (default: index=True) specifies whether to write the index column. Default is True which means the index column is written. If you don’t want to write the index column, then index=False should be used. data.to_csv(‘elections.csv’)

Pickle

pickle is a Python module used to serialize and deserialize Python objects. It can be used to store and retrieve Python objects from disk.

Serialization

Serialization is the process of converting a Python object into a byte stream. This byte stream can be stored on disk or sent over a network.

The pickle.dump() function is used to serialize a Python object. It takes two arguments: the object to serialize and a file object to write the byte stream to.

import pickle

data = {'name': 'Alice', 'age': 25}

with open('data.pickle', 'wb') as f:
    pickle.dump(data, f)

In this example, we serialize a dictionary containing a person’s name and age to a file called data.pickle.

Deserialization

Deserialization is the process of converting a byte stream back into a Python object.

The pickle.load() function is used to deserialize a Python object. It takes a file object containing the byte stream as an argument and returns the deserialized object.


with open('data.pickle', 'rb') as f:
    data = pickle.load(f)

print(data)

In this example, we deserialize the byte stream from the data.pickle file back into a Python object and print it.

Security

It is important to note that the pickle module is not secure. Deserializing untrusted data can lead to security vulnerabilities, as malicious code can be executed during deserialization. It is recommended to only deserialize data from trusted sources.