草庐IT

python - pandas.DataFrame.to_sql 中的最佳 chunksize 参数

使用需要转储到PostgreSQL表中的大型pandasDataFrame。从我读过的内容来看,一次转储不是一个好主意,(我正在锁定数据库)而不是使用chunksize范围。答案here对工作流有帮助,但我只是问影响性能的block大小的值。In[5]:df.shapeOut[5]:(24594591,4)In[6]:df.to_sql('existing_table',con=engine,index=False,if_exists='append',chunksize=10000)是否有推荐的默认值?将参数设置得更高或更低时,性能是否存在差异?假设我有内存来支持更大的block大小

python - 在 Pandas read_csv 期间标记化数据时出错。如何真正看到坏线?

我有一个很大的csv,我按如下方式加载df=pd.read_csv('my_data.tsv',sep='\t',header=0,skiprows=[1,2,3])我在加载过程中遇到了几个错误。首先,如果我不指定warn_bad_lines=True,error_bad_lines=False,我会得到:Errortokenizingdata.Cerror:Expected22fieldsinline329867,saw24其次,如果我使用上面的选项,我现在得到:CParserError:Errortokenizingdata.Cerror:EOFinsidestringstarti

Python Pandas 系列失败日期时间

我认为这一定是pandas的失败,它有一个pandas系列(也有v.18.1和19),如果我为系列分配一个日期,第一次将它添加为int(错误),第二次添加为日期时间(正确),我不明白原因。例如使用这段代码:importdatetimeasdtimportpandasaspdseries=pd.Series(list('abc'))date=dt.datetime(2016,10,30,0,0)series["Date_column"]=dateprint("Thedateis{}andthetypeis{}".format(series["Date_column"],type(seri

python - to_excel() read_excel() 出现 Pandas Unicode 导入导出错误

早上好。我将一个更大的情况浓缩为以下内容:我有一个包含数据框的文件,其中包含一些值。df=pd.DataFrame({'joe':[['dog'],['cat'],['fish'],['rabbit']],'ben':[['dog'],['fish'],['fish'],['bear']]})df:benjoe0[dog][dog]1[fish][cat]2[fish][fish]3[bear][rabbit]此数据框中包含的数据类型如下:type(df.iloc[2,1]),df.iloc[2,1]>>>(list,['fish'])当我使用pd.to_excel()将数据框保存到e

python - 如何在 Pandas 数据框的每一行上使用 .rolling()?

我创建了一个Pandas数据框df:df.head()Out[1]:ABDateTime2010-01-0150.662365101.0350992010-01-012010-01-0247.65242499.2742882010-01-022010-01-0351.38745999.7471352010-01-032010-01-0452.34478899.6218962010-01-042010-01-0547.10636498.2862242010-01-05我可以添加A列的移动平均值:df['A_moving_average']=df.A.rolling(window=50,a

python - 将几列分组,然后在 Pandas 中聚合一组列(与 R 的 data.table 相比,它严重崩溃)

我是Python世界的新手,正在尝试将其用作数据分析的后备平台。我通常使用data.table来满足我的数据分析需求。问题是,当我对大型CSV文件(随机化、压缩、上传到http://www.filedropper.com/ddataredact_1)运行组聚合操作时,Python抛出:groupingpandasreturngetattr(obj,method)(*args,**kwds)ValueError:negativedimensionsarenotallowed或者(我什至遇到过...)File"C:\Anaconda3\lib\site-packages\pandas\co

Python Pandas : can't find numpy. core.multiarray 导入 Pandas 时

我正在尝试让我的代码(在eclipse中运行)导入pandas。我收到以下错误:"ImportError:numpy.core.multiarrayfailedtoimport"当我尝试导入pandas时。我正在使用python2.7、pandas0.7.1和numpy1.5.1 最佳答案 只是为了确保:您是否从源代码安装了pandas?确保它使用的是所需的NumPy版本。安装pandas后是否升级了NumPy?确保重新编译pandas,因为ABI可能会发生一些变化(但是对于那个版本的NumPy,我怀疑是这种情况)您是从源目录调用p

python - 从索引到列级别交换一级(Pandas Multiindex Dataframe)

我有一个多索引的pandas.Dataframe,它是这样的:BAZPALFooBar1241AB2CD1341EF2GH我需要以适当的方式将一级索引与列交换。我需要以这样的方式结束:124134CooBarBAZ1AE2CGPAL1BF2DH 最佳答案 您需要取消堆叠现有索引级别Foo,堆叠所需列“Coo”,然后重新排列索引级别。交换索引级别后,您可能想要对其进行排序。作为最后一步,您可能想要删除所有值的列名称(val)。df=(pd.DataFrame({'Foo':[124,124,134,134]*2,'Bar':[1,2,

python - 如何向量化基于最后 x 行数据的 Pandas 计算

我有一个相当复杂的预测代码,其中有超过20列,每列有数百万个数据,使用wls。现在我使用iterrow循环遍历日期,然后根据这些日期和这些日期中的值,提取不同大小的数据进行计算。在我的生产中运行需要几个小时,我将代码简化为以下内容:importpandasaspdimportnumpyasnpfromdatetimeimporttimedeltadf=pd.DataFrame(np.random.randn(1000,2),columns=list('AB'))df['dte']=pd.date_range('9/1/2014',periods=1000,freq='D')defcal

python - Pandas 滚动 OLS 被弃用

当我运行旧代码时,我收到以下警告:“pandas.stats.ols模块已弃用,将在未来版本中删除。我们引用外部包,如statsmodels”。我不明白statsmodel中是否有一个用户友好的滚动OLS模块。pandas.stats.ols模块的优点在于,您可以轻松说明是否需要拦截、窗口类型(滚动、扩展)和窗口长度。有没有完全相同的模块?例如:YY=DataFrame(np.log(np.linspace(1,10,10)),columns=['Y'])XX=DataFrame(np.transpose([np.linspace(1,10,10),np.linspace(‌​2,10