我已经使用 python 一段时间了,但我缺少一些东西。
导入模块时,如果模块包含类或仅包含定义,有什么区别吗?例如,我有以下两个模块:
def hello():
print("hello")
Run Code Online (Sandbox Code Playgroud)
或者
class Hello():
def hello():
print("hello")
Run Code Online (Sandbox Code Playgroud)
我从另一个模块导入它
import module_name
module_name.hello()
Run Code Online (Sandbox Code Playgroud)
或者
import module_name
Hello = module_name.Hello()
Hello.hello()
Run Code Online (Sandbox Code Playgroud)
代码有什么不同吗?
如果想并行执行怎么办?如果我只导入定义会有什么问题吗?
我需要在matplotlib中绘制表格。问题是某些列具有一级标题,某些列具有双层标题。
这是我需要的:
这是一级标题的简单示例:
df = pd.DataFrame()
df['Animal'] = ['Cow', 'Bear']
df['Weight'] = [250, 450]
df['Favorite'] = ['Grass', 'Honey']
df['Least Favorite'] = ['Meat', 'Leaves']
df
Run Code Online (Sandbox Code Playgroud)
fig = plt.figure(figsize=(9,2))
ax=plt.subplot(111)
ax.axis('off')
table = ax.table(cellText=df.values, colColours=['grey']*df.shape[1], bbox=[0, 0, 1, 1], colLabels=df.columns)
plt.savefig('Table.jpg')
Run Code Online (Sandbox Code Playgroud)
最后一段代码产生下一张图片:
要拥有所需的桌子,我需要进行哪些更改?
我有一个像这样的Spark DataFrame:
+-----+--------+-------+-------+-------+-------+-------+
| Type|Criteria|Value#1|Value#2|Value#3|Value#4|Value#5|
+-----+--------+-------+-------+-------+-------+-------+
| Cat| 1| 1| 2| 3| 4| 5|
| Dog| 2| 1| 2| 3| 4| 5|
|Mouse| 4| 1| 2| 3| 4| 5|
| Fox| 5| 1| 2| 3| 4| 5|
+-----+--------+-------+-------+-------+-------+-------+
Run Code Online (Sandbox Code Playgroud)
您可以使用下一个代码重现它:
data = [('Cat', 1, 1, 2, 3, 4, 5),
('Dog', 2, 1, 2, 3, 4, 5),
('Mouse', 4, 1, 2, 3, 4, 5),
('Fox', 5, 1, 2, 3, 4, 5)]
columns = ['Type', 'Criteria', 'Value#1', 'Value#2', 'Value#3', 'Value#4', 'Value#5']
df = …Run Code Online (Sandbox Code Playgroud)