Comma-Separated Values are csv Abbreviation, table data is stored in a text file. When we are dealing with a .csv file with python, found csv with pandas Kit Kit than a lot easier, here are some of the basic operations, such as read and write (read, write) and slice (slice).
Write (write) operation:
import pandas as pd
Each list represents # inside a csv file
A = [. 1, 2,. 3]
B = [. 4,. 5,. 6]
C = [. 7,. 8,. 9]
# Dictionary key value column names in the csv
csv_file = pd.DataFrame ({ 'x' : a, 'y': b, 'z': c})
# The csv_file saved as test.csv, index indicating whether the line name, default = True
csv_file.to_csv ( "test.csv", index = False)
The following shows the contents test.csv file:
X, Y, Z
1,4,7
2,5,8
3,6,9
read (Read) operation easier:
= pd.read_csv rows ( 'test.csv')
Print (rows)
which is read out of rows of data:
x y z
. 1. 4. 7 0
. 1 2. 8. 5
2. 3. 6. 9
The following is a slice (Slice) Operation:
row_slice = rows.iloc [0: 2, 1: 3] # fetch rows 0-1 rows 1-2 column element
Print (row_slice)
row_slice data:
z
0 4 7
1 5 8