且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为数据框中的每一列创建一个csv文件?

更新时间:2022-12-08 22:59:06

  • 因为您已经有一个数据框
  • 将数据框的索引设置为日期"
  • 遍历每列并将其保存到csv.
    • 使用 df [col]
    • 为每一列选择数据
    • csv文件名将为 f'{col} .csv',其中 col 是列名.
      • Since you already have a dataframe
      • Set the index of the dataframe as the 'Date' column
      • Iterate through each column and save it to a csv.
        • Select the data for each column with df[col]
        • The csv file name will be f'{col}.csv', where col is the column name.
          • df.to_csv(os.getcwd()+'\\ file.csv')进入 AppData 文件夹
          • 当前工作目录不一定是您想要保存文件的位置.
          • 指定完整的所需保存路径,例如'c:/Users/<< user_name>>/Documents/{col} .csv'.
          • df.to_csv(os.getcwd()+'\\file.csv') goes into the AppData folder
          • The current working directory is not necessarily where you want the file saved.
          • Specify the full desired save path, 'c:/Users/<<user_name>>/Documents/{col}.csv', for example.
        import pandas as pd
        
        # set Date as the index
        df.set_index('Date', inplace=True)
        
        # iterate through each column and save it
        for col in df.columns:
            df[col].to_csv(f'c:/Users/<<user_name>>/Documents/{col}.csv', index=True)