调用CSV文件并将文件中的百分数转换为float,以及计算列的平均数,方差
CSV文件就长这个亚子。
import numpy as py
import pandas as pd
import matplotlib.pyplot as plt
#sep=';',是因为CSV文件是以;为分隔符的
history = pd.read_csv(r'C:\Users\Administrator\Desktop\history.csv',sep=';',index_col = 'date')
history.head(3)
#查看数据类型
print(history.dtypes)
#将文件中某一列的object格式转换为float模式,不然字符串是无法计算的
history['Emerging'] = history['Emerging'].str.strip('%').astype(float)/100
print(history.dtypes)
#计算某一列的平均数
history.Emerging.mean()
#计算某一列的第10分位数和第90分位数
[history.Emerging.quantile(i) for i in [0.1,0.9]]
#继续将另外两列数据转换一下格式
history['Relative Value'] = history['Relative Value'].str.strip('%').astype(float)/100
history['Fixed Income Arbitrage'] = history['Fixed Income Arbitrage'].str.strip('%').astype(float)/100
history['Relative Value'].plot()
history['Fixed Income Arbitrage'].plot()