我认为第三个选项应该是剥离空白的最快方法?在处理大型数据集时,有人可以给我一些我应该应用的一般规则吗?我通常使用.astype(str)但很明显,对于我知道已经是对象的列来说这是不值得的.
%%timeit
fcr['id'] = fcr['id'].astype(str).map(str.strip)
10 loops, best of 3: 47.8 ms per loop
%%timeit
fcr['id'] = fcr['id'].map(str.strip)
10 loops, best of 3: 25.2 ms per loop
%%timeit
fcr['id'] = fcr['id'].str.strip(' ')
10 loops, best of 3: 55.5 ms per loop
Run Code Online (Sandbox Code Playgroud) 我希望在我的IPython幻灯片中有交互式小部件,就像我在其他演示文稿中看到的那样.我知道如何创建Slide单元格,我已经能够将一个套牌导出为独立的reveal.js HTML套牌.但是,这个套牌并没有连接到内核.有些人如何能够将现场笔记本放入幻灯片模式并使用交互式小部件?
我正在尝试对 2 个地理数据框进行空间连接。
\n\n两个索引都是这种类型:\nRangeIndex(start=0, stop=312, step=1)
\n\n我收到以下错误:
\n\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n<ipython-input-228-d0d0190e2940> in <module>()\n----> 1 Sales_new_data_concastinate_SR_coundaries_joines=gpd.sjoin(Sales_new_data_concastinated,SR_locations, op=\'within\',how=\'left\')\n\n~/anaconda3/lib/python3.7/site-packages/geopandas/tools/sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)\n 51 or any(right_df.columns.isin([index_left, index_right]))):\n 52 raise ValueError("\'{0}\' and \'{1}\' cannot be names in the frames being"\n---> 53 " joined".format(index_left, index_right))\n 54 \n 55 # the rtree spatial index only allows limited (numeric) index types, but an\n\nValueError: \'index_left\' and \'index_right\' cannot be names in the frames being joined\nRun Code Online (Sandbox Code Playgroud)\n\n来源https://github.com/geopandas/geopandas/blob/master/geopandas/tools/sjoin.py
\n\n说: …
我正在尝试从ascii读取几百个表,然后将它们写入mySQL.使用Pandas似乎很容易,但我遇到了一个对我没有意义的错误:
我有一个8列的数据框.这是列列表/索引:
metricDF.columns
Index([u'FID', u'TYPE', u'CO', u'CITY', u'LINENO', u'SUBLINE', u'VALUE_010', u'VALUE2_015'], dtype=object)
Run Code Online (Sandbox Code Playgroud)
然后我用to_sql它将数据附加到mySQL
metricDF.to_sql(con=con, name=seqFile, if_exists='append', flavor='mysql')
Run Code Online (Sandbox Code Playgroud)
我得到一个关于列"nan"的奇怪错误:
OperationalError: (1054, "Unknown column 'nan' in 'field list'")
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的所有列都有名称.我意识到mysql/sql支持写在开发中出现所以也许这就是原因?如果有的话有解决方法吗?任何建议将不胜感激.
有没有办法限制默认线程调度程序使用的内核数(使用dask数据帧时默认)?
使用compute,您可以使用以下命令指定它:
df.compute(get=dask.threaded.get, num_workers=20)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有办法将其设置为默认值,因此您不需要为每次compute调用指定此项?
例如,在小型集群(例如64个核心)的情况下会很有趣,但是与其他人共享(没有作业系统),并且我不希望在使用dask开始计算时占用所有核心.
我有一个带有几何列和带有点名称的列的地理数据框“all_locations”。在地图上绘制点效果很好,但我想用位置名称注释点。
['位置'] ['几何']
BUITHVN8 POINT()
(当然,实际数据框要大得多)
我试过这个(和其他事情):
all_locations['coords'] = all_locations['geometry'].apply(lambda x: x.point.coords[:])
all_locations['coords'] = [coords[0] for coords in all_locations['coords']]
all_locations.plot(ax=ax)
for idx, row in all_locations.iterrows():
plt.annotate(s=row['locatie'], xy=row['geometry'])
Run Code Online (Sandbox Code Playgroud)
添加坐标列但它给出了这个错误:“点”对象没有属性“点”
当使用pandas的内置绘图功能绘制时间序列时,它似乎忽略了我的索引的时区:它始终使用x轴的UTC时间.一个例子:
import numpy as np
import matplotlib.pyplot as plt
from pandas import rolling_mean, DataFrame, date_range
rng = date_range('1/1/2011', periods=200, freq='S', tz="UTC")
data = DataFrame(np.random.randn(len(rng), 3), index=rng, columns=['A', 'B', 'C'])
data_cet = data.tz_convert("CET")
# plot with data in UTC timezone
fig, ax = plt.subplots()
data[["A", "B"]].plot(ax=ax, grid=True)
plt.show()
# plot with data in CET timezone, but the x-axis remains the same as above
fig, ax = plt.subplots()
data_cet[["A", "B"]].plot(ax=ax, grid=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
情节不会改变,尽管指数有:
In [11]: data.index[0]
Out[11]: <Timestamp: 2011-01-01 00:00:00+0000 UTC, tz=UTC> …Run Code Online (Sandbox Code Playgroud) 我有一个多边形的功能集合,我必须首先在临时文件中写入然后加载它geopandas.GeoDataFrame.from_file(tmp_json_file),有没有办法不写临时文件,只是GeoDataFrame从GeoJSON对象创建?
python ggplot很棒,但还是新的,我发现需要回退传统的matplotlib技术来修改我的情节.但我不知道如何将轴实例传递给ggplot,或者从中获取一个实例.
所以让我说我建立一个这样的情节:
import ggplot as gp
Run Code Online (Sandbox Code Playgroud)
(明确导入)
p = gp.ggplot(gp.aes(x='basesalary', y='compensation'), data = df)
p + gp.geom_histogram(binwidth = 10000)
Run Code Online (Sandbox Code Playgroud)
到目前为止没问题.但现在让我们说我希望y轴在对数范围内.我希望能够这样做:
plt.gca().set_yscale('log')
Run Code Online (Sandbox Code Playgroud)
不幸的是,plt.gca()没有访问创建的轴ggplot.我最终得到两个数字:来自ggplot的线性刻度的直方图,以及带有对数刻度y轴的空图.
我已经试过这两个数的变化gca()而gcf()没有成功.
我使用Python 3(我也安装了Python 2),我想从短文本中提取国家或城市.例如,text = "I live in Spain"或text = "United States (New York), United Kingdom (London)".
各国答案:
我试图安装,geography但我无法运行pip install geography.我收到此错误:
收集地理位置找不到满足要求地理位置的版本(来自版本:)没有找到地理位置的匹配分布
它看起来geography只适用于Python 2.
我也有geopandas,但我不知道如何使用geopandas从文本中提取所需的信息.