在Pandas 0.17中,我尝试按特定列排序,同时保持层次索引(A和B).B是通过串联设置数据帧时创建的运行编号.我的数据如下:
C D
A B
bar one shiny 10
two dull 5
three glossy 8
foo one dull 3
two shiny 9
three matt 12
Run Code Online (Sandbox Code Playgroud)
这就是我需要的:
C D
A B
bar two dull 5
three glossy 8
one shiny 10
foo one dull 3
three matt 12
two shiny 9
Run Code Online (Sandbox Code Playgroud)
下面是我正在使用的代码和结果.注意:Pandas 0.17会警告dataframe.sort将被弃用.
df.sort_values(by="C", ascending=True)
C D
A B
bar two dull 5
foo one dull 3
bar three glossy 8
foo three matt 12
bar one shiny 10
foo …Run Code Online (Sandbox Code Playgroud) 关于将多索引的级别[0]切换为级别1的范围有很多帖子.但是,我无法找到解决问题的方法; 也就是说,我需要level [0]索引值的1级索引范围
dataframe:首先是A到Z,Rank是1到400; 我需要每个级别[0](第一个)的前2个和后2个,但不是在同一个步骤中.
Title Score
First Rank
A 1 foo 100
2 bar 90
3 lime 80
4 lame 70
B 1 foo 400
2 lime 300
3 lame 200
4 dime 100
Run Code Online (Sandbox Code Playgroud)
我试图用下面的代码获取每个1级索引的最后2行,但它仅适用于第一级[0]值.
[IN] df.ix[x.index.levels[1][-2]:]
[OUT]
Title Score
First Rank
A 3 lime 80
4 lame 70
B 1 foo 400
2 lime 300
3 lame 200
4 dime 100
Run Code Online (Sandbox Code Playgroud)
我通过交换索引得到的前两行,但我不能使它适用于最后两行.
df.index = df.index.swaplevel("Rank", "First")
df= df.sortlevel() #to sort by Rank …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Django/Heroku入门教程部署我的第一个使用Django/Heroku的示例应用程序.
我的工具:Python 3.4和Windows 7 PowerShell.
我的挑战:部署到Heroku失败了,我不知道为什么.在我第一次"git push"时,我看到默认情况下使用了python-2.7.0.然后我runtime.txt在app根目录中添加了一个(python-3.4.0)文件.
这是我跑步时会发生的事情 git push heroku master
-----> Python app detected
-----> Preparing Python runtime (python-3.4.0)
-----> Installing Setuptools (2.1)
-----> Installing Pip (1.5.4)
-----> Installing dependencies using Pip (1.5.4)
Exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/commands/install.py", line 262, in run
for req in parse_requirements(filename, finder=finder, options=options, session=session):
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/req.py", line 1546, in parse_requirements
session=session,
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/download.py", line 275, in get_file_content
content …Run Code Online (Sandbox Code Playgroud) I am trying to open JSON files located in a directory other than the current working directory (cwd). My setting: Python3.5 on Windows (using Anaconda).
from pathlib import *
import json
path = Path("C:/foo/bar")
filelist = []
for f in path.iterdir():
filelist.append(f)
for file in filelist:
with open(file.name) as data_file:
data = json.load(data_file)
Run Code Online (Sandbox Code Playgroud)
In this setting I have these values:
file >> C:\foo\bar\0001.json
file.name >> 0001.json
Run Code Online (Sandbox Code Playgroud)
However, I get the following error message:
---> 13 with open(file.name) as data_file:
14 …Run Code Online (Sandbox Code Playgroud) GeoPandas 在幕后使用 Shapely。为了获得最近的邻居,我看到使用了nearest_points from shapely。然而,这种方法不包括 k 最近点。
我需要计算从 GeoDataFrames 到最近点的距离,并将距离插入包含“从该点开始”数据的 GeoDataFrame 中。
这是我GeoSeries.distance()在不使用其他包或库的情况下使用的方法。请注意,k == 1返回值实质上显示的是到最近点的距离。@cd98 还提供了仅针对最近点的 GeoPandas 解决方案,这启发了我的方法。
这对于我的数据效果很好,但我想知道使用 shapely 或sklearn.neighbors是否有更好或更快的方法或其他好处?
import pandas as pd
import geopandas as gp
gdf1 > GeoDataFrame with point type geometry column - distance from this point
gdf2 > GeoDataFrame with point type geometry column - distance to this point
def knearest(from_points, to_points, k):
distlist = to_points.distance(from_points)
distlist.sort_values(ascending=True, inplace=True) # To have the closest ones …Run Code Online (Sandbox Code Playgroud)