Every time I analyze data, I forget how to handle cvs data, so I would like to make a note of it.
import pandas as pd
pd.read_csv('data.csv')
ʻUnicodeDecodeError:'utf-8' codec can't decode byte 0x8e in position 0: invalid start byte` and I got an error.
pd.read_csv('data.csv' , encoding='cp932')
When I added ʻencoding ='cp932'` to the end, it was displayed properly.
Get only one row or one column of data from csv
df = pd.read_csv('data.csv' , encoding='cp932')
df.iloc[0,:] #Extract all data in the first line
df.iloc[:,2] #Extract all data in the third column
[Slice of line name / line number]: Extract multiple lines
df[1:4] #Extract from the 1st line to the 3rd line
Add all the data in one column to get the total
df.iloc[:,3].sum() #Calculate the total of the data in the 4th column
When you want to divide the column data of csv data by a certain number
#Divide each of the numbers in the third column of the csv data by 2.
w = df.iloc[:,2]
for i in w:
d = i / 2
print(d)
Recommended Posts