python常用語法筆記
阿新 • • 發佈:2018-12-04
1.註釋:
#單行註釋
ctrl + / 多行註釋
2.對csv檔案進行讀取
方法1:
import pandas as pd import matplotlib.pyplot as plt import numpy as np import pywt import csv from pandas import DataFrame df = pd.DataFrame(pd.read_csv(r'C:\Users\jiaodanyang\Desktop\PHM\data\diabetes.csv')) data = [] data = df.loc[:,'preg'] #iloc是根據所在列的位置進行索引,loc是根據列名進行索引 print(data)
方法2:
path = r'C:\Users\jiaodanyang\Desktop\PHM\data\diabetes.csv'
data_input = []
csvFile = open(path,'r', newline='') # 設定newline,否則兩行之間會空一行
reader= csv.reader(csvFile)
for item in reader:
data_input.append(item)
print(data_input)
方法3:
with open(path,'r') as f: #1.建立閱讀器物件 reader = csv.reader(f) #2.讀取檔案第一行資料 head_row=next(reader) print(head_row) data_attribute = [] for item in head_row: data_attribute.append(item) print(data_attribute)