[PYTHON] Extract csv data and calculate

Handle csv data

Every time I analyze data, I forget how to handle cvs data, so I would like to make a note of it.

load csv

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.

Extract columns / rows from csv data

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

Get a row for pandas.DataFrame

[Slice of line name / line number]: Extract multiple lines

df[1:4] #Extract from the 1st line to the 3rd line

Calculate the total for each column

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

Calculate csv data with for statement

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

Extract csv data and calculate
Extract Twitter data with CSV
Extract Pokemon GO Pokemon data and skill data
Extract data from S3
Read and write csv file
Reading and writing csv files
Follow Blender's data structure and extract vertex coordinates from fbx
Extract and package initrd images
Point and Figure Data Modeling
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
Algebraic data types and FizzBuzz
Prepare Backtrader CSV data and try running the sample script
Read zipline brand data from csv file and perform back test
Extract database tables with CSV [ODBC connection from R and python]
Grayscale image and save as csv
Extract specific data from complex JSON
Memo "Chapter 5 – Dictionaries and Structuring Data"
Parsing CSV format data using SQL
Python CSV file reading and writing
Hashing data in R and Python
About time series data and overfitting
Overlay and visualize Geo data and statistical data
Algebraic data types and object-oriented programming
Reading and writing CSV with Python
Extract the TOP command result with USER and output it as CSV
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
Extract and plot the latest population data from the PDF data provided by the city