我有一个条形图,它看起来像我想要的样子,除了 y 轴上的科学记数法之外。
其他一些解决方案包括使用
ax.yaxis.set_major_formatter(tick)
Run Code Online (Sandbox Code Playgroud)
这不起作用。另外,我尝试检查这是否是一个偏移问题,但它应该显示一个“+”号,但在本例中没有。
每当我使用:
plt.ticklabel_format(style='plain')
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\matplotlib\axes\_base.py", line 2831, in ticklabel_format
self.xaxis.major.formatter.set_scientific(sb)
AttributeError: 'FixedFormatter' object has no attribute 'set_scientific'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Python/Projects/Kaggle 1.py", line 13, in <module>
plt.ticklabel_format(style='plain')
File "C:\Python\lib\site-packages\matplotlib\pyplot.py", line 2982, in ticklabel_format
useMathText=useMathText)
File "C:\Python\lib\site-packages\matplotlib\axes\_base.py", line 2856, in ticklabel_format
"This method only works with the ScalarFormatter.")
AttributeError: This method only works with the ScalarFormatter.
Run Code Online (Sandbox Code Playgroud)
我研究过这个 ScalarFormatter,但我无法更清楚地了解它为什么不起作用。我尝试将其显式包含在代码中,但它不起作用。 …
我有一个包含以下内容的文件:
Blekota blaboli o koblihach.
Blanka je bl...
GEwI
er
Run Code Online (Sandbox Code Playgroud)
我需要替换以Bl或bl开头的每个单词xxxx并将其保存到新文件中。我尝试了这个,但没有成功。
Blekota blaboli o koblihach.
Blanka je bl...
GEwI
er
Run Code Online (Sandbox Code Playgroud)
期望的输出是:
xxxx xxxx o koblihach.
xxxx je xxxx...
GEwI
er
Run Code Online (Sandbox Code Playgroud)
请问我做错了什么?
我有以下问题。我的数据框中有一个 date_time 列(以及许多其他列)。
df["Date_time"].head()
0 2021-05-15 09:54
1 2021-05-27 17:04
2 2021-05-27 00:00
3 2021-05-27 09:36
4 2021-05-26 18:39
Name: Date_time, dtype: object
Run Code Online (Sandbox Code Playgroud)
我想将此列分成两部分(日期和时间)。
我使用这个效果很好的公式:
df["Date"] = ""
df["Time"] = ""
def split_date_time(data_frame):
for i in range(0, len(data_frame)):
df["Date"][i] = df["Date_time"][i].split()[0]
df["Time"][i] = df["Date_time"][i].split()[1]
split_date_time(df)
Run Code Online (Sandbox Code Playgroud)
但有没有更优雅的方式呢?谢谢
我有以下问题。我需要在 plt.hist() 的 X 轴上将标签居中。我在这里找到了一些答案:How to center labels in histogramplot建议使用align = "left/mid/right" . 但是,它没有给我正确的输出:
plt.hist(data['Col1'], align='left')

plt.hist(data['Col1'], align='mid')

plt.hist(data['Col1'], align='right')

我想让ok和18 let正好位于每个小节的中间。请问我该如何修复它?
整个代码:
plt.style.use('ggplot')
plt.xticks(rotation='vertical')
plt.locator_params(axis='y', integer=True)
plt.suptitle('My histogram', fontsize=14, fontweight='bold')
plt.ylabel('Frequency', fontweight='bold')
plt.hist(data['Col1'], align='mid')
plt.show()
Run Code Online (Sandbox Code Playgroud) 我想用 Python 获取 pandas df 的数据库。我使用以下代码:
self.cursor = self.connection.cursor()
query = """
SELECT * FROM `an_visit` AS `visit`
JOIN `an_ip` AS `ip` ON (`visit`.`ip_id` = `ip`.`ip_id`)
JOIN `an_useragent` AS `useragent` ON (`visit`.`useragent_id` = `useragent`.`useragent_id`)
JOIN `an_pageview` AS `pageview` ON (`visit`.`visit_id` = `pageview`.`visit_id`)
WHERE `visit`.`visit_id` BETWEEN %s AND %s
"""
self.cursor.execute(query, (start_id, end_id))
df = pd.DataFrame(self.cursor.fetchall())
Run Code Online (Sandbox Code Playgroud)
该代码有效,但我也想获取列名。我尝试了这个问题MySQL: Get column name or alias from query
但这不起作用:
fields = map(lambda x: x[0], self.cursor.description)
result = [dict(zip(fields, row)) for row in self.cursor.fetchall()]
Run Code Online (Sandbox Code Playgroud)
如何将 …
我是 Python 的初学者,我有以下关于生成器的问题。我的生成器产生三个变量:
def generate():
for i in range(1, 10):
j = i + 1
k = i ** 2
yield i, j, k
Run Code Online (Sandbox Code Playgroud)
在以下函数中,我只想循环遍历变量j。当然,这有效:
for var_i, var_j, var_k in generate():
print("This is my varible j: ", var_j)
Run Code Online (Sandbox Code Playgroud)
但是我这里有两个未使用的变量 - var_i, var_k。所以我想问一下,如果有更好的方法,如何做到这一点?
这个答案对我没有帮助:循环遍历返回多个值的生成器 非常感谢。