pandas 打印到屏幕上正确,但只将一些数据保存到csv [英] Pandas prints to screen corrently but saves only some data to csv

查看:79
本文介绍了 pandas 打印到屏幕上正确,但只将一些数据保存到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import pandas as pd
import os, glob
from pandas import Panel
import sqlite3

my_dir = '/home/manish/Desktop/Equity/'
filelist = []
fileList = []
os.chdir(my_dir)

for files in glob.glob('*.txt'):
    p = pd.read_csv(files, names = ['Name', 'Date', 'Open', 'High',
               'Low', 'Close', 'Volume', 'Null'])
    del p['Null']
    print p
    p.to_csv('monthly.csv', sep = ',')

#Even this does not work
#p.to_csv('monthly.csv', sep = ',')

我的问题是数据已正确打印到屏幕上,但是当我将其保存到csv时,它只保存了特定日期的数据.

My problem is the data is printed correctly to screen, but when I save it to csv, it only saves the data for 1 particular day.

推荐答案

您可以打开文件一次即可编写,不需要一次存储所有数据,如果有很多数据,不可能:

You can open the file once and write as you go, you don't need to store all the data at once which if you had a lot of data may not be possible:

os.chdir(my_dir)
glb = glob.iglob('*.txt')

with open("monthly.csv", "w") as f:
    p= pd.read_csv(next(glb), names=['Name', 'Date', 'Open', 'High',
                                  'Low', 'Close', 'Volume', 'Null'])
    del p["Null"]
    p.to_csv(f)
    for files in glb:
        p = pd.read_csv(files, names=['Name', 'Date', 'Open', 'High',
                                      'Low', 'Close', 'Volume', 'Null'])
        del p["Null"]
        p.to_csv(f, sep=',', header=False)

您只需将文件对象和header=False传递给p.to_csv,以避免多次写入标头.

You just pass the file object to p.to_csv with header=False to avoid writing the header multiple times.

如果您不希望使用Null列,则可以传递usecols=[0, 1, 2, 3, 4, 5, 6]而不是稍后再删除该列:

If you don't want the Null column, you can pass usecols=[0, 1, 2, 3, 4, 5, 6] instead of deleting the column later:

with open("monthly.csv", "w") as f:
    p = pd.read_csv(next(glb), names=['Name', 'Date', 'Open', 'High',
                                      'Low', 'Close', 'Volume'], usecols=[0, 1, 2, 3, 4, 5, 6])
    p.to_csv(f)
    for files in glb:
        p = pd.read_csv(files, names=['Name', 'Date', 'Open', 'High',
                                      'Low', 'Close', 'Volume'], usecols=[0, 1, 2, 3, 4, 5, 6])
        p.to_csv(f, sep=',', header=False)

这篇关于 pandas 打印到屏幕上正确,但只将一些数据保存到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆