小编Dan*_*ty2的帖子

Python将HTML图像转换为PNG

给定带有图像的保存HTML文件(Bokeh的输出),如何将其保存为DPI为300的PNG文件?

我找到了类似问题的答案,但它们似乎对我不起作用.我想我需要有人来解释整个过程(导入所需的包,如果适用,它需要在哪里定位,以及如何调用它).

我在pip安装webkit2png之后尝试过这个:

import os
os.system("webkit2png" "texas.html")
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

import subprocess
subprocess.call("webkit2png", "texas.html")
Run Code Online (Sandbox Code Playgroud)

提前致谢!

html png python-3.x bokeh

2
推荐指数
1
解决办法
4732
查看次数

Pandas Concatenate Values From Different Rows

Given this dataframe:

import pandas as pd
a=pd.DataFrame({'number':[2,2,3],'A':['abc','def','ghi']})

a
    A   number
0   abc     2
1   def     2
2   ghi     3
Run Code Online (Sandbox Code Playgroud)

I need to concatenate values, in order of index, from rows with the same number value, separated by '; '.

Desired result:

    A           number
0   abc; def    2; 2
2   ghi         3
Run Code Online (Sandbox Code Playgroud)

So far, I thought I could isolate the dataframes and then somehow try to join them together like this:

a['rank']=a.groupby('number').rank()
a1=a.loc[a['rank']==1]
a2=a.loc[a['rank']==2]
b=a1.merge(a2,on='number',how='left')
b=b.fillna('')

b
    A_x     number  rank_x …
Run Code Online (Sandbox Code Playgroud)

python concatenation pandas

2
推荐指数
1
解决办法
2775
查看次数

熊猫格式列为货币

给定以下数据框:

import pandas as pd
df = pd.DataFrame(
        {'A':['A','B','C','D'],
         'C':[12355.00,12555.67,640.00,7000]
        })
df

    A   C
0   A   12355.00
1   B   12555.67
2   C   640.00
3   D   7000.00
Run Code Online (Sandbox Code Playgroud)

我想像这样将价值转换成千美元的美元:

    A   C
0   A   $12.3K
1   B   $12.5K
2   C    $0.6K
3   D    $7.0K
Run Code Online (Sandbox Code Playgroud)

我需要做的第二件事是以某种方式将它们放入Seaborn热图,该图仅接受浮点数和整数。有关热图方面的更多信息,请参见此处

我假设一旦将浮点数转换为货币,它们将采用对象格式,但我希望可以解决这一问题。

提前致谢!

python-3.x pandas seaborn

1
推荐指数
3
解决办法
4381
查看次数

散景映射县

我试图用密歇根州的县数据修改这个例子.简而言之,它是有效的,但它似乎在绘制县的过程中在这里和那里增加了一些额外的形状.我猜测在某些情况下(有岛屿的县),岛屿部分需要列为一个单独的"县",但我不确定另一个案例,例如在较低的韦恩县正确的部分国家.

这是我目前拥有的图片: 在此输入图像描述

这是我到目前为止所做的:

  1. 从Bokeh的样本县数据中获取县数据只是为了获得每个州号的州缩写(我的第二个,主数据源只有州号).对于这个例子,我将通过仅过滤状态号26来简化它.
  2. 美国人口普查网站获取各州的州立坐标('500k'文件).
  3. 使用以下代码生成密歇根州的"交互式"地图.

注意:要pip install shapefile(真的是pyshp),我想我必须从这里下载.whl文件,然后执行pip install [path to .whl file].

import pandas as pd
import numpy as np
import shapefile
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.palettes import Viridis6
from bokeh.plotting import figure, show, output_notebook
shpfile=r'Path\500K_US_Counties\cb_2015_us_county_500k.shp'
sf = shapefile.Reader(shpfile)
shapes = sf.shapes()

#Here are the rows from the shape file (plus lat/long coordinates)
rows=[]
lenrow=[]
for i,j in zip(sf.shapeRecords(),sf.shapes()):
    rows.append(i.record+[j.points])
    if len(i.record+[j.points])!=10:
           print("Found record with irrular number of …
Run Code Online (Sandbox Code Playgroud)

shapefile python-3.x pandas bokeh

1
推荐指数
1
解决办法
1850
查看次数

Matplotlib 绘制虚线圆圈(使用 plt.plot 而不是 plt.scatter)

鉴于以下情况:

import matplotlib.pyplot as plt 
import numpy as np 
#http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter
x = np.random.randn(60) 
y = np.random.randn(60)
x2 = np.random.randn(60)
y2 = np.random.randn(60)

plt.plot(x, y, marker='o', markeredgecolor='r', linestyle='none', markerfacecolor='none')
plt.plot(x2, y2, marker='o', markeredgecolor='r', linestyle='none', markerfacecolor='none')
plt.show()
Run Code Online (Sandbox Code Playgroud)

我希望将 x2 和 y2 绘制为虚线(甚至虚线)圆圈。我避免使用 plt.scatter 因为我的脚本的其余部分与 plt.plot 一起工作得更好。这是我要找的: 在此处输入图片说明

提前致谢!

仅供参考:这是我创建的实际图表。我现在只是使用六边形来表示不同的数据(未来数据)。

在此处输入图片说明

自定义图例和在熊猫数据框中的多组行上绘图增加了我无法使用 plt.scatter 克服的复杂性层。

python matplotlib markers

1
推荐指数
1
解决办法
1794
查看次数

Python将一个列表中的元素与另一个中的所有元素组合在一起

鉴于以下列表:

a = ['a','b']
b = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

我想生产这个:

c = ['a1','a2','a3','a4','b1','b2','b3','b4']
Run Code Online (Sandbox Code Playgroud)

所以我基本上想要将b的每个元素加入到a中的每个元素中.

我想要一个类似于此的方法:

[x+str(y) for x in a and y in b]
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python string list-comprehension list

1
推荐指数
1
解决办法
1386
查看次数

Matplotlib 绘制虚线圆圈

鉴于以下情况:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.random.randn(60) 
y = np.random.randn(60)
x2 = np.random.randn(60)
y2 = np.random.randn(60)

plt.scatter(x, y, s=80, facecolors='none', edgecolors='r')
plt.scatter(x2, y2, s=80, facecolors='none', edgecolors='r')
plt.show()
Run Code Online (Sandbox Code Playgroud)

我将如何用虚线圆圈绘制相同的数据(每个圆圈的轮廓是虚线而不是实线)仅用于 x 2 和 y2 ?

提前致谢!

更新: 我知道这可以用这里的补丁来完成,但如果可能的话,我需要通过 plt.scatter 来完成,因为我还将在同一图上绘制另一组圆圈并使用与图表尺寸混淆的补丁(太瘦了)。

matplotlib markers

0
推荐指数
1
解决办法
4116
查看次数

Django电子邮件-定义用户名和密码

文档中,我可以在后端文件中定义主机,端口,用户名和密码,但是我想在代码本身中定义所有它们。能做到吗?如果是这样,怎么办?

from django.core.mail import EmailMessage

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
)

message.attach_file('/images/weather_map.pdf')
Run Code Online (Sandbox Code Playgroud)

提前致谢!

更新:

我想避免将凭证存储在任何文件中。最终,我希望代码提示输入用户名和密码作为输入变量。 更新:

我尝试了这个:

import pandas as pd
from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend
attachment_path=r'C:\path'+'\\'

connection = EmailBackend(
    host='host',
    port=587,
    username='login',
    password='password'
)

email = EmailMessage(
    'Hello',
    'Body goes here',
    'example@example.com',
    ['example@example.com'],
    ['example@example.com'],
    reply_to=['example@example.com'],
    headers={'Message-ID': 'foo'},
    connection=connection
)
email.attach_file(attachment_path+'attachment.pdf')
email.send()
Run Code Online (Sandbox Code Playgroud)

python email django attachment

0
推荐指数
1
解决办法
320
查看次数