草庐IT

convert_encoding

全部标签

python - 值错误 : Cannot set a frame with no defined index and a value that cannot be converted to a Series

我在我的python3.X中使用Pandas0.20.3。我想在另一个Pandas数据框中的Pandas数据框中添加一列。两个数据框都包含51行。所以我使用了以下代码:class_df['phone']=group['phone'].values我收到以下错误消息:ValueError:CannotsetaframewithnodefinedindexandavaluethatcannotbeconvertedtoaSeriesclass_df.dtypes给我:Group_IDobjectYEARobjectTergetobjectphoneobjectageobject和type(

python / Pandas : convert month int to month name

我发现的大部分信息都不在python>pandas>dataframe中,因此是这个问题。我想将1到12之间的整数转换为缩写的月份名称。我有一个df,它看起来像:clientMonth1sss022yyy123www06我希望df看起来像这样:clientMonth1sssFeb2yyyDec3wwwJun 最佳答案 您可以通过结合使用calendar.month_abbr和df[col].apply()有效地做到这一点importcalendardf['Month']=df['Month'].apply(lambdax:calen

python - 属性错误 : 'list' object has no attribute 'encode'

我有一个unicode对象列表,想将它们编码为utf-8,但编码似乎不起作用。代码在这里:>>>tmp=[u'testcontext']>>>tmp.encode('utf-8')Traceback(mostrecentcalllast):File"",line1,inAttributeError:'list'objecthasnoattribute'encode'>>>我不明白为什么没有属性编码 最佳答案 您需要在tmp[0]上进行encode,而不是在tmp上。tmp不是字符串。它包含一个(Unicode)字符串。尝试运行typ

python - 网络x : Convert multigraph into simple graph with weighted edges

我有一个多图对象,并希望将其转换为带有加权边的简单图对象。我查看了networkx文档,似乎找不到一个内置函数来实现这一点。我只是想知道是否有人知道networkx中可以实现此目标的内置功能。我查看了to_directed()、to_undirected()函数,但它们不符合我的目标。 最佳答案 一种非常简单的方法就是将您的多重图作为输入传递给Graph。importnetworkxasnxG=nx.MultiGraph()G.add_nodes_from([1,2,3])G.add_edges_from([(1,2),(1,2),

Python numpy : cannot convert datetime64[ns] to datetime64[D] (to use with Numba)

我想将一个日期时间数组传递给一个Numba函数(它不能被矢量化,否则会很慢)。我了解Numba支持numpy.datetime64。但是,它似乎支持datetime64[D](天精度)但不支持datetime64[ns](纳秒精度)(我很难学到这一点:它有记录吗?)。我尝试将datetime64[ns]转换为datetime64[D],但似乎找不到方法!有什么想法吗?我用下面的最少代码总结了我的问题。如果您运行testdf(mydates),即datetime64[D],它可以正常工作。如果您运行testdf(dates_input),即datetime64[ns],则不会。请注意,此

Python Scrapy : Convert relative paths to absolute paths

我已经根据这里的伟人提供的解决方案修改了代码;我在这里得到代码下方显示的错误。fromscrapy.spiderimportBaseSpiderfromscrapy.selectorimportHtmlXPathSelectorfromscrapy.utils.responseimportget_base_urlfromscrapy.utils.urlimporturljoin_rfcfromdmoz2.itemsimportDmozItemclassDmozSpider(BaseSpider):name="namastecopy2"allowed_domains=["namastef

python - 类型错误 : only length-1 arrays can be converted to Python scalars while plot showing

我有这样的Python代码:importnumpyasnpimportmatplotlib.pyplotaspltdeff(x):returnnp.int(x)x=np.arange(1,15.1,0.1)plt.plot(x,f(x))plt.show()还有这样的错误:TypeError:onlylength-1arrayscanbeconvertedtoPythonscalars我该如何解决? 最佳答案 当函数需要单个值但您传递一个数组时,会引发错误“只有长度为1的数组可以转换为Python标量”。np.int是内置int的别

python - "OverflowError: Python int too large to convert to C long"在 windows 但不是 mac

我在windows和mac上运行完全相同的代码,使用python3.564位。在Windows上,它看起来像这样:>>>importnumpyasnp>>>preds=np.zeros((1,3),dtype=int)>>>p=[6802256107,5017549029,3745804973]>>>preds[0]=pTraceback(mostrecentcalllast):File"",line1,inpreds[0]=pOverflowError:PythoninttoolargetoconverttoClong但是,这段代码在我的mac上运行良好。任何人都可以帮助解释原因或为

python - 如何在python中解码(双重) 'url-encoded'字符串

尝试通过以下方式解码url-encoded字符串some_string='FireShot3%2B%25282%2529.png'importurllibres=urllib.unquote(some_string).decode()resu'FireShot3+%282%29.png'原始字符串是FireShot3(2).png。任何帮助将不胜感激。回答:urllib.unquote_plus(urllib.unquote_plus(some_string))由于双重编码。 最佳答案 您的输入被编码双。使用Python3:urll

python - UnicodeEncodeError : 'ascii' codec can't encode character u'\xa3'

我正在阅读一个Excel电子表格,其中包含一些£符号。当我尝试使用xlrd模块读取它时,我收到以下错误:x=table.cell_value(row,col)x=x.decode("ISO-8859-1")UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xa3'inposition0:ordinalnotinrange(128)如果我将其重写为x.encode('utf-8')它将停止抛出错误,但不幸的是,当我将数据写入其他地方(如latin-1)时,£符号都变成了乱码。如何解决此问题并正确读取£符号?---更新---一些善良