调用CSV文件并将文件中的百分数转换为float,以及计算列的平均数,方差

那年仲夏 提交于 2020-01-26 03:55:02

调用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()

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!