我的Mongo数据库中有这样的结构:
{'_id':'...',
'friends':
{'id1': {'name1':'value1', 'name2':'value2'},
'id2': {'name1':'', 'name2':''},
...}
}
Run Code Online (Sandbox Code Playgroud)
如何通过name1(value1)在此词典(朋友)中找到元素(朋友)?
我有一个包含日期时间和整数的数据框
import numpy as np
import pandas as pd
df = pd.DataFrame()
df['dt'] = pd.date_range("2017-01-01 12:00", "2017-01-01 12:30", freq="1min")
df['val'] = np.random.choice(xrange(1, 100), df.shape[0])
Run Code Online (Sandbox Code Playgroud)
给我
dt val
0 2017-01-01 12:00:00 33
1 2017-01-01 12:01:00 42
2 2017-01-01 12:02:00 44
3 2017-01-01 12:03:00 6
4 2017-01-01 12:04:00 70
5 2017-01-01 12:05:00 94*
6 2017-01-01 12:06:00 42*
7 2017-01-01 12:07:00 97*
8 2017-01-01 12:08:00 12
9 2017-01-01 12:09:00 11
10 2017-01-01 12:10:00 66
11 2017-01-01 12:11:00 71
12 2017-01-01 12:12:00 25
13 …Run Code Online (Sandbox Code Playgroud) 我有多个(大)JSON文件,我想添加到Firebase实时数据库(RTBD).我正在使用Geofire,因此所有子节点都需要在同一个父节点下.我正在存储静态地理数据,但我想定期刷新它.如果我无法将其刷新,那么始终保证我正在更新的一个大JSON总是100%完成就变得有问题了.
我想要的数据结构是这样的:
{"parent" : {"child_a" : 1, "child_b" : 2}
Run Code Online (Sandbox Code Playgroud)
我的第一个JSON看起来像这样
$ cat 1.json
{"parent": {"child_a" : 1 }}
Run Code Online (Sandbox Code Playgroud)
而我的第二个喜欢这个
$ cat 2.json
{"parent": {"child_b" : 2 }}
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用firebase-import与--merge标志:
$ firebase-import --database_url https://my_db.firebaseio.com/ --path / --json 2.json --service_account auth.json --merge
Run Code Online (Sandbox Code Playgroud)
并尝试使用firebase-tools更新标志:
$ firebase database:update / 2.json
Run Code Online (Sandbox Code Playgroud)
但是两者都从数据库中删除了1.json的内容,并给了我这个
{"parent": {"child_b" : 2 }}
Run Code Online (Sandbox Code Playgroud)
如何在不覆盖以前的JSON内容的情况下将多个JSON文件合并到同一节点?
部分答案:使用set带merge所示,这里消防店.但是我如何使用CLI和Fire 基础做到这一点?
说我有以下数据框
import pandas as pd
df = pd.DataFrame({ 'distance':[2.0, 3.0, 1.0, 4.0],
'velocity':[10.0, 20.0, 5.0, 40.0] })
Run Code Online (Sandbox Code Playgroud)
给出数据框
distance velocity
0 2.0 10.0
1 3.0 20.0
2 1.0 5.0
3 4.0 40.0
Run Code Online (Sandbox Code Playgroud)
如何计算距离列的滚动总和上速度列的平均值?在上面的示例中,在最后N行上创建一个滚动总和,以使最小累积距离为5,然后计算这些行上的平均速度。
我的目标输出将如下所示:
distance velocity rv
0 2.0 10.0 NaN
1 3.0 20.0 15.0
2 1.0 5.0 11.7
3 4.0 40.0 22.5
Run Code Online (Sandbox Code Playgroud)
哪里
15.0 = (10+20)/2 (2 because 3 + 2 >= 5)
11.7 = (10 + 20 + 5)/3 (3 because 1 + 3 + 2 >= 5) …Run Code Online (Sandbox Code Playgroud) 我有一个配置文件,其中包含一个以制表符分隔的字符串.我想检索该字符串,然后将其转换为一个很好的列表.但是,当我直接在iPython上做这件事时,我看到了一些我看不到的有趣的东西.
[myvars]
myString = "a\tb\tc\td"
.....
.....<many more variables>
Run Code Online (Sandbox Code Playgroud)
我的Python代码有这个:
param_dict = dict(config.items(myvars))
str1 = param_dict["myString"]
print str1
print str1.split()
Run Code Online (Sandbox Code Playgroud)
它打印出来:
"a\tb\tc\td"
['"a\\tb\\tc\\td"']
Run Code Online (Sandbox Code Playgroud)
但是,当我在我的python控制台上做同样的事情时,我得到了我的期望:
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "a\tb\tc\td".split()
['a', 'b', 'c', 'd']
>>> k = "a\tb\tc\td"
>>> k.split()
['a', 'b', 'c', 'd']
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?有人可以帮我吗?我无法更改配置文件变量的格式.而且,我想把变量拿出来并剥离成一个很好的列表.
谢谢.
我有一个包含这样数据的 Excel 文件
Fruits Description
oranges This is an orange
apples This is an apple
oranges This is also oranges
plum this is a plum
plum this is also a plum
grape I can make some wine
grape make it red
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码将其转换为字典
import pandas as pd
import xlrd
file = 'example.xlsx'
x1 = pd.ExcelFile(file)
print(x1.sheet_names)
df1 = x1.parse('Sheet1')
#print(df1)
print(df1.set_index('Fruits').T.to_dict('list'))
Run Code Online (Sandbox Code Playgroud)
当我执行上述我得到错误
UserWarning: DataFrame columns are not unique, some columns will be omitted.
Run Code Online (Sandbox Code Playgroud)
我想要一本看起来像下面的字典
{'oranges': ['this is an orange', 'this is …Run Code Online (Sandbox Code Playgroud) 要从列表中删除重复的列表,Python 中有几种不错的方法 -例如:
a = [[ 9.1514622, 47.1166004 ], [ 9.1513045, 47.1164599 ], [ 9.1516278, 47.1163001 ], [ 9.1517832, 47.1164408 ], [ 9.1514622, 47.1166004 ] ]
print len(a) # 5
b_set = set(map(tuple,a))
b = map(list,b_set)
print len(b) # 4
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我不得不将我的列表转换为Shapely Polygon对象,我需要在其中简化几何图形并执行一些其他地理功能。
from shapely.geometry import Polygon
a = [[[ 9.1514622, 47.1166004 ], [ 9.1513045, 47.1164599 ], [ 9.1516278, 47.1163001 ], [ 9.1517832, 47.1164408 ], [ 9.1514622, 47.1166004 ] ] ]
polys = [Polygon(item) for item in …Run Code Online (Sandbox Code Playgroud) 我有两个包含互斥物品的清单 - 我们为这些清单选择油和水基液体,因为这些自然不能混合:
waters = ['tea', 'lemonade', 'juice', 'pepsi']
oils = ['olive oil', 'corn oil', 'peanut oil']
Run Code Online (Sandbox Code Playgroud)
我想测试foo是否只包含水清单或油类清单中的项目,但不包括两者.这样:
foo = ['tea', 'corn oil'] => FAIL
bar = ['pepsi', 'tea', 'juice'] => PASS
baz = ['olive oil'] => PASS
Run Code Online (Sandbox Code Playgroud)
到目前为止我的尝试:
def contains_conflicting_types(test, targets, conflicts):
intarget = False
inconflict = False
for t in test:
if t in targets:
intarget = True
if t in conflicts:
inconflict = True
if intarget and inconflict:
return True
return False
#Usage:
contains_conflicting_types(['A','B'], ['A','1'], ['B','2']) #returns …Run Code Online (Sandbox Code Playgroud) 我正在基于此页面使用简单的标记来开发Python Folium的简单实现。
import folium
map_1 = folium.Map(location=[45.372, -121.6972], zoom_start=12,
tiles='Stamen Terrain')
map_1.simple_marker([45.3288, -121.6625], popup='Mt. Hood Meadows')
map_1.simple_marker([45.3311, -121.7113], popup='Timberline Lodge')
map_1.create_map(path='mthood.html')
Run Code Online (Sandbox Code Playgroud)
我可以在弹出窗口中包含HTML,但是我想嵌入一个网页。这是我的模型。

可能吗?
As of Pandas 0.19.2, the function read_csv() can be passed a URL. See, for example, from this answer:
import pandas as pd
url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c=pd.read_csv(url)
Run Code Online (Sandbox Code Playgroud)
The URL I'd like to use is: https://moz.com/top500/domains/csv
With the above code, this URL returns an error:
urllib2.HTTPError: HTTP Error 403: Forbidden
Run Code Online (Sandbox Code Playgroud)
based on this post, I can get a valid response by passing a request header:
import urllib2,cookielib
site= "https://moz.com/top500/domains/csv"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', …Run Code Online (Sandbox Code Playgroud) Python3具有超级string.format打印:
'{} {}'.format('one', 'two')
Run Code Online (Sandbox Code Playgroud)
如果我的字符串在数组中,则一种方法是将它们键入:
a = ['one','two']
'{} {}'.format(a[0],a[1])
Run Code Online (Sandbox Code Playgroud)
但是,如何从数组中打印,而不必键入每个元素呢?
例如,损坏的代码:
a = ['one','two']
'{} {}'.format(a)
Run Code Online (Sandbox Code Playgroud)
给我一个预期的错误: IndexError: tuple index out of range
当然,玩','.join(a)不会有所帮助,因为它给出的是一个字符串而不是2。
(或者有没有办法用f弦更好地做到这一点?)
对于完全公开,我使用的是原始字符串,因为它具有某些几何意义,而我的真实代码如下所示:
hex_string = r'''
_____
/ \
/ \
,----( {} )----.
/ \ / \
/ {} \_____/ {} \
\ / \ /
\ / \ /
)----( {} )----(
/ \ / \
/ \_____/ \
\ {} / \ {} /
\ / \ /
`----( {} )----' …Run Code Online (Sandbox Code Playgroud) Docker 需要登录到“Hub”才能访问安装包的下载。
如何在不注册的情况下在 Windows 和 Mac 上下载最新版本?
python ×9
pandas ×4
contains ×1
dataframe ×1
dictionary ×1
docker ×1
firebase ×1
firebase-cli ×1
folium ×1
geofire ×1
geospatial ×1
grouping ×1
html ×1
http-headers ×1
installation ×1
javascript ×1
leaflet ×1
list ×1
mongodb ×1
numpy ×1
polygon ×1
printing ×1
pymongo ×1
python-2.7 ×1
python-3.x ×1
shapely ×1
split ×1
time-series ×1