How do you manipulate CSV files using python? Flashcards

(19 cards)

1
Q

What does CSV stand for?

A

Comma-Separated Values. It’s a plain text format where data is organized in rows, with values in each row separated by a delimiter (like a comma).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the built-in Python module for handling CSVs?

A

The csv module. (import csv)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

csv: How do you open a CSV file for reading?

A

Use with open(‘filename.csv’, mode=’r’) as file:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

csv: What object do you create to read the file row by row?

A

A csv.reader object: \ncsv_reader = csv.reader(file)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

csv: When iterating over a csv.reader, what data type is each row?

A

A list of strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

csv: How do you read a CSV file where the first row is a header?

A

Use csv.DictReader. Each row will be a dictionary where keys are the header names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

csv: When opening a file for writing with the csv module, what two arguments are crucial?

A

mode=’w’ (to write) and newline=’’ (to prevent blank lines between rows on Windows).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

csv: What object do you create to write to a CSV file?

A

A csv.writer object: \ncsv_writer = csv.writer(file)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

csv: How do you write a single row of data (as a list)?

A

csv_writer.writerow([‘data1’, ‘data2’, ‘data3’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

csv: How do you write multiple rows at once (from a list of lists)?

A

csv_writer.writerows(list_of_lists)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

csv: How do you specify a different delimiter (like a semicolon)?

A

Pass it as an argument: \ncsv.reader(file, delimiter=’;’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the most popular third-party library for CSV manipulation, especially for data analysis?

A

The pandas library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

pandas: How do you read a CSV file into a DataFrame?

A

import pandas as pd \ndf = pd.read_csv(‘filename.csv’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pandas: What is a DataFrame?

A

A 2D labeled data structure, like a spreadsheet or SQL table, with columns and rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

pandas: How do you access a single column (a “Series”) from a DataFrame?

A

df[‘column_name’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

pandas: How do you filter rows (e.g., all rows where ‘age’ > 30)?

A

df[df[‘age’] > 30]

17
Q

pandas: How do you add a new column to the DataFrame?

A

df[‘new_column_name’] = [value1, value2, …]

18
Q

pandas: How do you save a DataFrame back to a new CSV file?

A

df.to_csv(‘new_file.csv’)

19
Q

pandas: When using to_csv(), how do you prevent it from saving the DataFrame’s row numbers (the index)?

A

df.to_csv(‘new_file.csv’, index=False)