草庐IT

composite-index

全部标签

python - pandas DataFrame reset_index 可以处理重复的列名?

是否有任何等效于pandas.DataFrame.reset_index()的操作,可以处理列名重复的情况?我希望它丢弃列名并为列返回默认编号索引0,1,2..。(当我有重复的列名时,df.rename或df.reindex_axis等方法不起作用。)示例输入:pd.DataFrame(np.random.rand(5,3),columns=['A','A','B'])AAB00.50.30.910.70.90.320.90.40.830.60.20.940.70.40.6预期输出:01200.80.10.210.40.20.420.30.30.430.40.10.841.00.90.

python - psycopg2 "IndexError: tuple index out of range"使用带有参数元组的 '%' 之类的运算符时出错

这很好用:cc.execute("select*frombookswherenamelike'%oo%'")但是如果第二个参数通过:cursor.execute("select*frombookswherenamelike'%oo%'OFFSET%LIMIT%",(0,1))心理错误:Traceback(mostrecentcalllast):File"",line1,inIndexError:tupleindexoutofrange如何避免这个错误? 最佳答案 首先,您应该使用%%来插入%文字,否则,库将尝试使用所有%作为占位符。

python - 为什么在尝试使用 "List index out of range"在列表中添加连续数字时得到 "for i in list"?

这个问题在这里已经有了答案:HowcanIiterateoveroverlapping(current,next)pairsofvaluesfromalist?(12个答案)WhydoIgetanIndexError(orTypeError,orjustwrongresults)from"ar[i]"inside"foriinar"?(4个答案)关闭4个月前。给定以下列表a=[0,1,2,3]我想创建一个新列表b,它由a的当前值和下一个值相加的元素组成。它将包含比a少1的元素。像这样:b=[1,3,5](从0+1、1+2和2+3)这是我尝试过的:b=[]foriina:b.append

python - pandas dataframe group year index by decade

假设我有一个索引为每月时间步长的数据框,我知道我可以使用dataframe.groupby(lambdax:x.year)将每月数据分组为每年并应用其他操作。有什么方法可以快速对它们进行分组,比方说按十年分组?感谢任何提示。 最佳答案 要得到十年,您可以将年份除以10,然后乘以10。例如,如果您从>>>dates=pd.date_range('1/1/2001',periods=500,freq="M")>>>df=pd.DataFrame({"A":5*np.arange(len(dates))+2},index=dates)>>

python - 使用 Python 解析 Thread-Index 邮件头

一些邮件客户端,不设置Referencesheaders,而是设置Thread-Index。有没有办法在Python中解析这个header?相关:Howdoestheemailheaderfield'thread-index'work?邮件1Date:Tue,2Dec201408:21:00+0000Thread-Index:AdAOBz5QJ/JuQSJMQTmSQ8+dVs2IDg==邮件2(与邮件1相关)Date:Mon,8Dec201413:12:13+0000Thread-Index:AdAOBz5QJ/JuQSJMQTmSQ8+dVs2IDgE4StZw更新我希望能够在我的

python - 使用 Python “pip” : Cannot fetch index base URL http://安装时出错

我正在尝试安装本地版本的ScrumDo进行测试。只有这样我才能在我的安装中找到必须运行的pip:sourcebin/activatepipinstall-rrequirements.txt我得到错误:Downloading/unpackingdjango-storagesCannotfetchindexbaseURLhttp://b.pypi.python.org/simple/Couldnotfindanydownloadsthatsatisfytherequirementdjango-storages根本没有找到django-storages的发行版将完整的日志存储在./pip-l

python - findspark.init() 索引错误 : list index out of range error

在Python3.5Jupyter环境中运行以下命令时,出现以下错误。关于造成它的原因有什么想法吗?importfindsparkfindspark.init()错误:IndexErrorTraceback(mostrecentcalllast)in()1importfindspark---->2findspark.init()34importpyspark/.../anaconda/envs/pyspark/lib/python3.5/site-packages/findspark.pyininit(spark_home,python_path,edit_rc,edit_profil

python - 从 {index : list of row values} 形式的字典构造 Pandas DataFrame

我已经设法使用:dft=pd.DataFrame.from_dict({0:[50,45,00,00],1:[53,48,00,00],2:[56,53,00,00],3:[54,49,00,00],4:[53,48,00,00],5:[50,45,00,00]},orient='index')这样做,构造函数看起来就像DataFrame一样,易于阅读/编辑:>>>dft0123050450015348002565300354490045348005504500但是DataFrame.from_dictconstructor没有列参数,因此为列提供合理的名称需要一个额外的步骤:dft.

python - Sklearn 线性回归 - "IndexError: tuple index out of range"

我有一个“.dat”文件,其中保存了X和Y的值(所以一个元组(n,2),其中n是行数)。importnumpyasnpimportmatplotlib.pyplotaspltimportscipy.interpolateasinterpfromsklearnimportlinear_modelin_file=open(path,"r")text=np.loadtxt(in_file)in_file.close()x=np.array(text[:,0])y=np.array(text[:,1])我为linear_model.LinearRegression()创建了一个实例,但是当我调

python - NumPy 是否有 unravel_index() 的反函数?

numpy.unravel_index()将一个形状和一个平面索引放入数组中,并返回表示数组中该索引的元组。有倒数吗?我可以手动计算它,但这似乎一定是某处的内置函数...... 最佳答案 从numpy1.6.0(2011年5月)开始,有一个内置的NumPy函数ravel_multi_indexConvertsatupleofindexarraysintoanarrayofflatindices,applyingboundarymodestothemulti-index.(用户BiRico的评论中也提到了这一点,但实际上应该作为答案出