找到关于 Python 的10786 篇文章

Python Pandas 中按升序排列索引

AmitDiwan
更新于 2021年9月21日 06:23:07

741 次浏览

sort_index() 用于按升序和降序排列索引。如果不指定任何参数,则索引按升序排列。首先,导入所需的库 - import pandas as pd 创建一个新的 DataFrame。它具有未排序的索引 - dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1']) 排序索引 - dataFrame.sort_index() 示例以下是代码 - import pandas as pd dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1']) print"DataFrame...",dataFrame print"Sort index...",dataFrame.sort_index() 输出这将产生以下输出 - DataFrame... Col1 4 100 8 150 2 200 9 250 15 250 11 500 Sort index... Col1 2 200 4 100 8 150 9 250 11 500 15 250

如何在 matplotlib 中从一组点绘制最大的多边形?

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:49:14

486 次浏览

要在 matplotlib 中从一组点绘制最大的多边形,我们可以采取以下步骤 - 从 matplotlib.patches 导入“Polygon”。设置图形大小并调整子图之间和周围的填充。为最大的多边形创建数据点列表。获取多边形实例。创建一个图形和一组子图。添加多边形实例补丁。设置 x 和 y 缩放限制。要显示图形,请使用 show() 方法。示例import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Polygon plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = np.array([[1, 1], [0.5, 1.5], [2, 1], [1, 2], [2, ... 阅读更多

如何使用 Matplotlib 更改绘图的填充颜色?

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:32:03

7K+ 次浏览

要使用 matplotlib 更改绘图的填充颜色,我们可以采取以下步骤 - 设置图形大小并调整子图之间和周围的填充。使用 numpy 创建 x 和 y 数据点。创建一个图形和一组子图。使用 plot() 方法绘制 x 和 y 数据点,颜色为黄色,线宽为 7。使用 set_facecolor() 设置轴的填充颜色。要显示图形,请使用 show() 方法。示例from matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x ... 阅读更多

使用 Python、Numpy 和 Matplotlib 绘制带掩码的曲面图

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:23:51

578 次浏览

要使用 Python、Numpy 和 Matplotlib 绘制带掩码的曲面图,我们可以采取以下步骤 - 设置图形大小并调整子图之间和周围的填充。创建一个新图形或激活现有图形。将“ax”添加到图形作为子图排列的一部分。从坐标向量 pi 和 theta 返回坐标矩阵。使用带掩码数据点的创建 x、y 和 z。使用 x、y 和 z 数据点创建曲面图。要显示图形,请使用 show() 方法。示例import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = ... 阅读更多

Matplotlib – 使用 Networkx 绘制晶格和图形

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:19:25

1K+ 次浏览

要使用 networkx 绘制晶格和图形,我们可以采取以下步骤 - 导入 networkx 和 pyplot。设置图形大小并调整子图之间和周围的填充。使用 nx.grid_2d_graph(3, 3) 获取二维网格图。网格图的每个节点都与其四个最近的邻居连接。使用 Matplotlib 绘制图形 G。要显示图形,请使用 show() 方法。示例# Import networkx and pyplot import networkx as nx from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Draw the graph G = nx.grid_2d_graph(3, 3) nx.draw(G, node_size=100) plt.show() 输出它 ... 阅读更多

如何在 matplotlib 的散点图中设置点的边框颜色?

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:07:59

5K+ 次浏览

要在 matplotlib 散点图中设置点的边框颜色,我们可以采取以下步骤 - 设置图形大小并调整子图之间和周围的填充。初始化变量“N”以存储样本数据数量。使用 numpy 创建 x 和 y 数据点。使用 scatter() 方法绘制 x 和 y 数据点。要设置点的边框颜色,请在 scatter() 方法中使用 edgecolors 参数。在这里,我们使用 edgecolors='red' 将点的边框颜色设置为“红色”。要显示图形,请使用 show() 方法。示例import numpy as np from matplotlib import pyplot ... 阅读更多

Python - 在 Pandas DataFrame 中为列名添加前缀

AmitDiwan
更新于 2021年9月21日 06:16:21

246 次浏览

要为所有列名添加前缀,请使用 add_prefix() 方法。首先,导入所需的 Pandas 库 - import pandas as pd 创建一个包含 4 列的 DataFrame - dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] }) 使用 add_prefix() 为每列添加前缀 "_column" - dataFrame.add_prefix('column_') 示例以下是代码 - import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": ... 阅读更多

如何确定 matplotlib 条形图中条形的顺序?

Rishikesh Kumar Rishi
更新于 2021年9月20日 13:01:06

6K+ 次浏览

要确定 matplotlib 条形图中条形的顺序,我们可以采取以下步骤 - 设置图形大小并调整子图之间和周围的填充。创建一个二维、大小可变、可能异构的表格数据的 DataFrame,df。向当前图形添加子图。使用 DataFrame df 制作条形图。向当前图形添加子图。通过列标记创建另一个 DataFrame,df_sorted。使用 df_sorted 制作条形图。要显示图形,请使用 show() 方法。示例import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( ... 阅读更多

Matplotlib – 日期操作,使年份刻度每 12 个月显示一次

Rishikesh Kumar Rishi
更新于 2021年9月21日 11:34:04

3K+ 次浏览

为了使 matplotlib 日期操作能够每 12 个月显示一次年份刻度,我们可以采取以下步骤 - 设置图形大小并调整子图之间和周围的填充。使用 Pandas、Numpy 和 matplotlib 日期创建 d、y、s、years、months、monthsFmt 和 yearsFmt。在 DateFormatter 中使用“%B”显示完整的月份名称。在 DateFormatter 中使用“%Y”显示年份。创建一个新图形或激活现有图形。将“ax”添加到图形作为子图排列的一部分。使用 plot() 方法绘制“dts”和“s”数据点。设置次要或主要轴定位器和格式化器。将 minor_locator 设置为 months ... 阅读更多

Python - 反转 Pandas DataFrame 的列顺序

AmitDiwan
更新于 2021年9月20日 12:55:41

621 次浏览

要反转列顺序,请使用 dataframe.columns 并设置为 -1 - dataFrame[dataFrame.columns[::-1] 首先,导入所需的库 - import pandas as pd 创建一个包含 4 列的 DataFrame - dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] }) 反转列顺序 - df = dataFrame[dataFrame.columns[::-1]] 示例以下是代码 - import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, ... 阅读更多

广告
© . All rights reserved.