pandas 拆分数据集,然后创建折线图

dsf9zpds  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(79)

我正在处理一个7列宽、2516行长的数据集。有10个独特的公司,每行指的是一天的股票代码高,低,成交量和收盘。我试图创建一个线图与PLT或SNS图形10线在同一个图,1为每个公司,随着时间的推移(许多年)。我不知道从何说起。我是否需要为10家公司中的每一家创建一个新的df,或者我可以根据数据集中的公司进行拆分?我想画的是股票的收盘价和时间

fsi0uk1n

fsi0uk1n1#

要创建一个包含多条线的折线图,以反映每个公司的收盘股价随时间变化,您可以执行以下步骤:
1.将数据集读入pandas DataFrame。
1.如果数据集还没有表示时间的列(例如日期),请使用pd.to_datetime()将相关列转换为datetime数据类型。
1.根据每个公司将数据集拆分为单独的DataFrame。您可以使用pandas中的groupby()函数按company列对数据进行分组,并为每个公司创建DataFrames字典。
1.在DataFrames字典上迭代,并使用线图(例如,plt.plot()sns.lineplot())绘制每个公司的收盘价与时间的关系。
1.根据需要自定义打印,例如添加标签、图例和标题。
1.使用plt.show()显示绘图。
下面是一个示例代码片段演示了这个过程:

import pandas as pd
import matplotlib.pyplot as plt

# Read the dataset into a pandas DataFrame
df = pd.read_csv("your_dataset.csv")  # Replace with the actual dataset filename and path

# Convert the relevant column to a datetime data type
df['date'] = pd.to_datetime(df['date'])  # Replace 'date' with the actual column name for the time variable

# Split the dataset into separate DataFrames based on each company
company_dfs = dict(list(df.groupby('company')))  # Replace 'company' with the actual column name for the company variable

# Plotting the closing prices for each company
for company, company_df in company_dfs.items():
    plt.plot(company_df['date'], company_df['close'], label=company)  # Replace 'close' with the actual column name for the closing price

# Customize the plot
plt.xlabel('Date')
plt.ylabel('Closing Price')
plt.title('Closing Stock Prices over Time')
plt.legend()

# Display the plot
plt.show()

字符串
确保用数据集的实际文件名和路径替换'your_dataset.csv'。此外,调整列名('date', 'company', and 'close')以匹配数据集中的相应列。
此代码假设您的数据集有一个日期/时间列(例如'date')和一个公司名称列(例如'company')。它还假设收盘价的列名为'close'。根据需要调整这些列名以匹配您的数据集。

相关问题