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

dsf9zpds  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(165)

我正在处理一个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()显示绘图。
下面是一个示例代码片段演示了这个过程:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # Read the dataset into a pandas DataFrame
  4. df = pd.read_csv("your_dataset.csv") # Replace with the actual dataset filename and path
  5. # Convert the relevant column to a datetime data type
  6. df['date'] = pd.to_datetime(df['date']) # Replace 'date' with the actual column name for the time variable
  7. # Split the dataset into separate DataFrames based on each company
  8. company_dfs = dict(list(df.groupby('company'))) # Replace 'company' with the actual column name for the company variable
  9. # Plotting the closing prices for each company
  10. for company, company_df in company_dfs.items():
  11. plt.plot(company_df['date'], company_df['close'], label=company) # Replace 'close' with the actual column name for the closing price
  12. # Customize the plot
  13. plt.xlabel('Date')
  14. plt.ylabel('Closing Price')
  15. plt.title('Closing Stock Prices over Time')
  16. plt.legend()
  17. # Display the plot
  18. plt.show()

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

展开查看全部

相关问题