我查看了统计模型的示例,但没有看到很多将交叉验证应用于时间序列的示例。
假设我有这样的东西
`In [1]: from __future__ import print_function
In [2]: import numpy as np
In [3]: import statsmodels.api as sm
import pandas as pd
from statsmodels.tsa.arima_process import arma_generate_sample
np.random.seed(12345)
In [4]: import pandas as pd
In [5]: from statsmodels.tsa.arima_process import arma_generate_sample
In [6]: np.random.seed(12345)
In [7]: arparams = np.array([.75, -.25])
In [8]: maparams = np.array([.65, .35])
In [9]:
In [9]: arparams = np.r_[1, -arparams]
In [10]: maparam = np.r_[1, maparams]
In [11]: nobs = 250
In [12]: y = arma_generate_sample(arparams, …Run Code Online (Sandbox Code Playgroud) 如何将现有xlsxExcel 文件转换为xls同时保留 Excel 文件格式?我使用 Anaconda Python 3,所以我不确定我可以使用xlutils......conda install xlutils由于很多不兼容,我无法安装它。所以现在我使用没有以下代码的代码xlutils.copy():
import xlrd, xlwt
wb = xlrd.open_workbook(my_xlsx_excel_file)
# wb = xlutils.copy(wb)
wb.save(my_xlsx_excel_file[:-1])
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
AttributeError: 'Book' object has no attribute 'save'
Run Code Online (Sandbox Code Playgroud)
谢谢!
是否可以将Spyder的IPython控制台设置为像Jupyter Notebook的IPython单元格一样可水平滚动?我知道我可以设置pandas选项(例如.pd.set_option('display.width', 1000)和pd.set_option('display.max_columns', 50)),但在这种情况下,我的最后一列的行位于我的第一列的行下.我希望列中的列彼此相邻.
谢谢!
我想unique在groupby聚合中使用,但我不想nan在unique结果中使用.
示例数据框:
df = pd.DataFrame({'a': [1, 2, 1, 1, pd.np.nan, 3, 3], 'b': [0,0,1,1,1,1,1],
'c': ['foo', pd.np.nan, 'bar', 'foo', 'baz', 'foo', 'bar']})
a b c
0 1.0000 0 foo
1 2.0000 0 NaN
2 1.0000 1 bar
3 1.0000 1 foo
4 nan 1 baz
5 3.0000 1 foo
6 3.0000 1 bar
Run Code Online (Sandbox Code Playgroud)
而且groupby:
df.groupby('b').agg({'a': ['min', 'max', 'unique'], 'c': ['first', 'last', 'unique']})
Run Code Online (Sandbox Code Playgroud)
结果是:
a c
min max unique first last …Run Code Online (Sandbox Code Playgroud) 我想从一个有两列的 csv 文件 (psc.csv) 中读取,如下所示:
cellname,scrambling
UER1100A,128
UER1100B,129
UER1100C,130
UER1300A,1
UER1300B,2
UER1300C,3
UER1400H,128
Run Code Online (Sandbox Code Playgroud)
并将整个文件放入一个字典中,这样字典将如下所示:
{'UER1100A': '128' , 'UER1100B': '129' , 'UER1100C': '130' , ...}
Run Code Online (Sandbox Code Playgroud)
我尝试使用csv如下模块,但它返回混合输出并在单独的字典中。解决办法是什么?
我的代码:
#!/usr/bin/python3
import csv
with open('psc.csv', newline='') as pscfile:
reader = csv.DictReader(pscfile)
for row in reader:
print(row)
Run Code Online (Sandbox Code Playgroud) 如何y-axis在数字和图表中修改比例?我想要这样的东西:my_figure.y_range.end = my_figure.y_range.end * 1.3
所以我想要更高一些y-axis.谢谢!
如何在 Windows 上获取系统环境变量?使用以下代码,我只获得用户环境变量:
os.environ['PATH']
Run Code Online (Sandbox Code Playgroud)
或者这返回相同的:
os.getenv('PATH')
Run Code Online (Sandbox Code Playgroud)
谢谢!
我可以使用以下代码在图上绘制我的单头数据框:
plt.table(cellText=df.round(4).values, cellLoc='center', bbox=[0.225, 1, 0.7, 0.15],
rowLabels=[' {} '.format(i) for i in df.index], rowLoc='center',
rowColours=['silver']*len(df.index), colLabels=df.columns, colLoc='center',
colColours=['lightgrey']*len(df.columns), colWidths=[0.1]*len(df.columns))
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否可以绘制具有多索引列的数据框?我想要两个单独的“行”用于我的多标题,因此一个标题行中的元组不好。如果可能的话,我想在两个标题上应用上述样式(颜色)(为多标题设置不同的颜色会很棒)。
这是一个示例数据框:
df = pd.DataFrame([[11, 22], [13, 23]],
columns=pd.MultiIndex.from_tuples([('main', 'sub_1'), ('main', 'sub_2')]))
Run Code Online (Sandbox Code Playgroud)
结果:
main
sub_1 sub_2
0 11 22
1 13 23
Run Code Online (Sandbox Code Playgroud) 我已经读过这个,这个和这个帖子但是我不知道为什么quotechar不起作用pd.read_csv()(Python 3,pandas 0.18.0和0.18.1).我怎么能读这样的数据帧:
"column1","column2", "column3", "column4", "column5", "column6"
"AM", 7, "1", "SD", "SD", "CR"
"AM", 8, "1,2 ,3", "PR, SD,SD", "PR ; , SD,SD", "PR , ,, SD ,SD"
"AM", 1, "2", "SD", "SD", "SD"
Run Code Online (Sandbox Code Playgroud)
我想要以下结果:
Out[116]:
column1 column2 column3 column4 column5 column6
0 AM 7 1 SD SD CR
1 AM 8 1,2 ,3 PR, SD,SD PR ; , SD,SD PR , ,, SD,SD
2 AM 1 2 SD …Run Code Online (Sandbox Code Playgroud) 我有这个数据帧df由两列的ID和Date:
ID Date
4 1/1/2008
3 1/1/2007
2 9/23/2010
2 6/3/1998
2 1/1/2001 # Note this date should be before "6/3/1998" for ID# 2
1 4/30/2003
Run Code Online (Sandbox Code Playgroud)
我要排序df的ID和Date降序排列(最大- >最小的),但是当我尝试下面的脚本这似乎不工作:
print df.sort_values(by=["ID", "Date"], ascending=["False", "False"])
Run Code Online (Sandbox Code Playgroud)
输出应按此降序排列:
ID Date
4 1/1/2008
3 1/1/2007
2 9/23/2010
2 1/1/2001
2 6/3/1998
1 4/30/2003
Run Code Online (Sandbox Code Playgroud)
知道如何按正确的降序对日期进行排序?
python ×10
pandas ×3
csv ×2
axis ×1
bokeh ×1
console ×1
dataframe ×1
dictionary ×1
excel ×1
group-by ×1
ipython ×1
matplotlib ×1
multi-index ×1
null ×1
python-3.x ×1
sorting ×1
spyder ×1
statsmodels ×1
time-series ×1
unique ×1
windows ×1
xls ×1
xlsx ×1