我试图在Python中水平组合一些JPEG图像.
我有3个图像 - 每个是148 x 95 - 见附件.我只是制作了3张相同的图像 - 这就是为什么它们是相同的.
我正在尝试使用以下代码水平加入它们:
import sys
from PIL import Image
list_im = ['Test1.jpg','Test2.jpg','Test3.jpg']
new_im = Image.new('RGB', (444,95)) #creates a new empty image, RGB mode, and size 444 by 95
for elem in list_im:
for i in xrange(0,444,95):
im=Image.open(elem)
new_im.paste(im, (i,0))
new_im.save('test.jpg')
Run Code Online (Sandbox Code Playgroud)
但是,这会产生附加的输出test.jpg
.
有没有办法水平连接这些图像,使test.jpg中的子图像没有显示额外的部分图像?
我正在寻找一种水平连接n个图像的方法.我想一般使用这个代码所以我更愿意:
我在Python 2.7中使用这个Pandas DataFrame.
File heat Farheit Temp_Rating
1 YesQ 75 N/A
1 NoR 115 N/A
1 YesA 63 N/A
1 NoT 83 41
1 NoY 100 80
1 YesZ 56 12
2 YesQ 111 N/A
2 NoR 60 N/A
2 YesA 19 N/A
2 NoT 106 77
2 NoY 45 21
2 YesZ 40 54
3 YesQ 84 N/A
3 NoR 67 N/A
3 YesA 94 N/A
3 NoT 68 39
3 NoY 63 46
3 YesZ 34 81
Run Code Online (Sandbox Code Playgroud)
我需要用 …
我正在尝试搜索Pandas Dataframe以查找缺少条目或NaN条目的位置.
这是我正在使用的数据框:
cl_id a c d e A1 A2 A3
0 1 -0.419279 0.843832 -0.530827 text76 1.537177 -0.271042
1 2 0.581566 2.257544 0.440485 dafN_6 0.144228 2.362259
2 3 -1.259333 1.074986 1.834653 system 1.100353
3 4 -1.279785 0.272977 0.197011 Fifty -0.031721 1.434273
4 5 0.578348 0.595515 0.553483 channel 0.640708 0.649132
5 6 -1.549588 -0.198588 0.373476 audio -0.508501
6 7 0.172863 1.874987 1.405923 Twenty NaN NaN
7 8 -0.149630 -0.502117 0.315323 file_max NaN NaN
Run Code Online (Sandbox Code Playgroud)
注意:空白条目是空字符串 - 这是因为数据框来自的文件中没有字母数字内容.
如果我有这个数据帧,我怎样才能找到包含NaN或空白条目的索引的列表?
我df1
在Pandas中有这个DataFrame():
df1 = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
print df1
A B C D
0.860379 0.726956 0.394529 0.833217
0.014180 0.813828 0.559891 0.339647
0.782838 0.698993 0.551252 0.361034
0.833370 0.982056 0.741821 0.006864
0.855955 0.546562 0.270425 0.136006
0.491538 0.445024 0.971603 0.690001
0.911696 0.065338 0.796946 0.853456
0.744923 0.545661 0.492739 0.337628
0.576235 0.219831 0.946772 0.752403
0.164873 0.454862 0.745890 0.437729
Run Code Online (Sandbox Code Playgroud)
我想检查是否df2
存在来自另一个dataframe()的任何行(所有列)df1
.这是df2
:
df2 = df1.ix[4:8]
df2.reset_index(drop=True,inplace=True)
df2.loc[-1] = [2, 3, 4, 5]
df2.loc[-2] = [14, 15, 16, 17]
df2.reset_index(drop=True,inplace=True)
print df2
A B …
Run Code Online (Sandbox Code Playgroud) 我试图迭代Python Pandas数据帧的行.在数据帧的每一行中,我试图通过列名引用行中的每个值.
这是我有的:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
print df
A B C D
0 0.351741 0.186022 0.238705 0.081457
1 0.950817 0.665594 0.671151 0.730102
2 0.727996 0.442725 0.658816 0.003515
3 0.155604 0.567044 0.943466 0.666576
4 0.056922 0.751562 0.135624 0.597252
5 0.577770 0.995546 0.984923 0.123392
6 0.121061 0.490894 0.134702 0.358296
7 0.895856 0.617628 0.722529 0.794110
8 0.611006 0.328815 0.395859 0.507364
9 0.616169 0.527488 0.186614 0.278792
Run Code Online (Sandbox Code Playgroud)
我使用这种方法进行迭代,但它只给了我解决方案的一部分 - 在每次迭代中选择一行后,如何通过列名访问行元素?
这是我想要做的:
for row in df.iterrows(): …
Run Code Online (Sandbox Code Playgroud) 我试图bbox_to_anchor()
在Python的matplotlib图中使用关键字.
这是我制作的一个非常基本的情节:
import matplotlib.pyplot as plt
x = [1,2,3]
plt.subplot(211)
plt.plot(x, label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(0, -0.15, 1, 0), loc=2, ncol=2, mode="expand", borderaxespad=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
它基于以下示例:http: //matplotlib.org/users/legend_guide.html#plotting-guide-legend
我正在尝试使用自动将图例放置在绘图之外bbox_to_anchor()
.在此示例中,bbox_to_anchor()
列出了4个参数.
在此特定示例(上图)中,图例位于图表下方,因此每次更改图表时需要手动输入数字-0.15(字体大小,轴标题删除等). 是否可以针对以下情况自动计算这4个数字?:
如果没有,是否有可能在Python中对这些数字做出好的猜测?
此外,在上面的示例代码中,我将最后2个数字设置bbox_to_anchor()
为1和0,因为我不明白它们是什么或它们如何工作.最后2个数字bbox_to_anchor()
是什么意思?
我在Python 2.7中有以下Pandas数据帧.
import pandas as pd
trial_num = [1,2,3,4,5]
sail_rem_time = ['11:33:11','16:29:05','09:37:56','21:43:31','17:42:06']
dfc = pd.DataFrame(zip(*[trial_num,sail_rem_time]),columns=['Temp_Reading','Time_of_Sail'])
print dfc
Run Code Online (Sandbox Code Playgroud)
数据框如下所示:
Temp_Reading Time_of_Sail
1 11:33:11
2 16:29:05
3 09:37:56
4 21:43:31
5 17:42:06
Run Code Online (Sandbox Code Playgroud)
此数据框来自*.csv文件.我使用Pandas将*.csv文件作为Pandas数据帧读入.当我使用时print dfc.dtypes
,它向我显示该列Time_of_Sail
具有数据类型object
.我想将此列转换为datetime
数据类型但我只想要时间部分 - 我不想要年,月,日.
我可以试试这个:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]
Run Code Online (Sandbox Code Playgroud)
但问题是,当我运行print dfc.dtypes
它时仍然显示列Time_of_Sail
是object
.
有没有办法将此列转换为只有时间的日期时间格式?
附加信息:
要创建上述数据框和输出,这也有效:
import pandas as pd
trial_num = [1,2,3,4,5]
sail_rem_time = ['11:33:11','16:29:05','09:37:56','21:43:31','17:42:06']
data = [
[trial_num[0],sail_rem_time[0]],
[trial_num[1],sail_rem_time[1]],[trial_num[2],sail_rem_time[2]],
[trial_num[3],sail_rem_time[3]] …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python在图像上绘制一个圆圈.我尝试使用PIL,但我想指定一个linewidth
.目前,PIL绘制一个圆圈,但边界太薄.
这就是我所做的.
对于测试图像:我在MS Paint中创建了一个1632 X 1200图像并将其填充为绿色.我叫它test_1.jpg.这是输入文件:
from PIL import Image, ImageDraw
im = Image.open('test_1.jpg')
width, height = im.size
eX, eY = 816,816 #Size of Bounding Box for ellipse
bbox = (width/2 - eX/2, height/2 - eY/2, width/2 + eX/2, height/2 + eY/2)
draw = ImageDraw.Draw(im)
bbox_L = []
for j in range(0,5):
bbox_L.append([element+j for element in bbox])
draw.ellipse(tuple(bbox_L[j]), outline ='white')
im.show()
Run Code Online (Sandbox Code Playgroud)
基本上,我试图绘制多个圆圈,这些圆圈将在同一个点上居中但具有不同的半径.我的想法是,这会产生更粗的线条效果.
问题:正如您所看到的,左下角和右上角太薄了.此外,各个圆圈之间也存在间隙(参见左上角和右下角).
圆圈的厚度各不相同.我正在寻找一个厚度均匀的圆圈.
问题: 有没有办法在Python上绘制一个圆圈,在像test_1.jpg这样的图像上,使用PIL,NumPy等来指定线条厚度?
数据具有以下属性
这是我所做的
enter code here
#Buying price generalization
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})
#Maintanace generalization
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4})
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})
#lug_boot generalization
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})
#Safety Generalization
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})
print(df.head())
Run Code Online (Sandbox Code Playgroud)
打印时显示:
Cannot compare types 'ndarray(dtype=int64)' and 'str'
Run Code Online (Sandbox Code Playgroud) 我在Python中有一个Pandas数据帧.数据框的内容来自此处.我略微修改了"单一"列中第一个字母的大小写.这是我有的:
import pandas as pd
df = pd.read_csv('test.csv')
print df
Position Artist Single Year Weeks
1 Frankie Laine I Believe 1953 18 weeks
2 Bryan Adams I Do It for You 1991 16 weeks
3 Wet Wet Wet love Is All Around 1994 15 weeks
4 Drake (feat. Wizkid & Kyla) One Dance 2016 15 weeks
5 Queen bohemian Rhapsody 1975/76 & 1991/92 14 weeks
6 Slim Whitman Rose Marie 1955 11 weeks
7 Whitney Houston i Will …
Run Code Online (Sandbox Code Playgroud)