如何将大型CSV加载到Python中,选择特定列并另存为新CSV?
我有一个CSV文件,大约有800万行,大小约为3gb。我有一个要保存到新CSV中的特定列的列表。我一直在尝试将Panda与Python结合使用,但我就是做不好 这是我一直在使用的代码:如何将大型CSV加载到Python中,选择特定列并另存为新CSV?,python,pandas,csv,Python,Pandas,Csv,我有一个CSV文件,大约有800万行,大小约为3gb。我有一个要保存到新CSV中的特定列的列表。我一直在尝试将Panda与Python结合使用,但我就是做不好 这是我一直在使用的代码: import pandas as pd df = pd.read_csv('MyFile.csv' , usecols = ['AAA','BBB','CCC',]) 在最后一个命令之后,终端行返回3个点,如下所示。。。。然后我尝试输入这个命令 df.to_csv('NewFile.csv', index=Fa
import pandas as pd
df = pd.read_csv('MyFile.csv' , usecols = ['AAA','BBB','CCC',])
在最后一个命令之后,终端行返回3个点,如下所示。。。。然后我尝试输入这个命令
df.to_csv('NewFile.csv', index=False)
但我收到以下错误:
file "<stdin>", line 2
df.to_csv('NewFile.csv', index=False)
^
SyntaxError: invalid syntax
任何帮助都将不胜感激。多谢各位
编辑:这是整个终端屏幕的文本
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> df=pd.read_csv('MyFile.csv' , usecols = ['AAA','BBB','CCC',]
... pd.df.to_csv('NewFile.csv', index=False)?
File "<stdin>", line 2
pd.df.to_csv('NewFile.csv', index=False)?
^
SyntaxError: invalid syntax
>>>
将3GB文件读入内存不是一个好主意,熊猫会这么做。我建议先使用awk之类的流媒体工具来过滤数据。将3GB文件读入内存不是一个好主意,熊猫会这么做。我建议先使用awk之类的流媒体工具来过滤您的数据。您是否尝试过以下方法:
df.to_csv (r'C:\Users\Ron\Desktop\NewFile.csv', index = False)
将C:\Users\Ron\Desktop\NewFile.csv替换为输出文件名。您是否尝试过类似的方法:
df.to_csv (r'C:\Users\Ron\Desktop\NewFile.csv', index = False)
将C:\Users\Ron\Desktop\NewFile.csv替换为输出文件名。我找到了这个解决方案。我找到一个代码来删除我不想要的列。所以复制了CSV并做到了这一点。这是我使用的Py文件:
import csv
input_file = 'input.csv'
output_file = 'output.csv'
cols_to_remove = [1, 4, 10, 11] # Column indexes to be removed (starts at 0)
cols_to_remove = sorted(cols_to_remove, reverse=True) # Reverse so we remove from the end first
row_count = 0 # Current amount of rows processed
with open(input_file, "r") as source:
reader = csv.reader(source)
with open(output_file, "w", newline='') as result:
writer = csv.writer(result)
for row in reader:
row_count += 1
print('\r{0}'.format(row_count), end='') # Print rows processed
for col_index in cols_to_remove:
del row[col_index]
writer.writerow(row)
我找到了这个解决办法。我找到一个代码来删除我不想要的列。所以复制了CSV并做到了这一点。这是我使用的Py文件:
import csv
input_file = 'input.csv'
output_file = 'output.csv'
cols_to_remove = [1, 4, 10, 11] # Column indexes to be removed (starts at 0)
cols_to_remove = sorted(cols_to_remove, reverse=True) # Reverse so we remove from the end first
row_count = 0 # Current amount of rows processed
with open(input_file, "r") as source:
reader = csv.reader(source)
with open(output_file, "w", newline='') as result:
writer = csv.writer(result)
for row in reader:
row_count += 1
print('\r{0}'.format(row_count), end='') # Print rows processed
for col_index in cols_to_remove:
del row[col_index]
writer.writerow(row)
您得到了语法错误,因为您尚未在终端中的下一行中关闭括号
>>> df=pd.read_csv('MyFile.csv' , usecols = ['AAA','BBB','CCC',]
您得到了语法错误,因为您尚未在终端中的下一行中关闭括号
>>> df=pd.read_csv('MyFile.csv' , usecols = ['AAA','BBB','CCC',]
我想这可能是个问题。我一年要处理这个文件三次,每次都很痛苦。我将不得不研究awk过滤器…也许我应该说文件是2.6Gb,不确定它是否真的重要,但我应该有更多的定义。我认为这可能是一个问题。我一年要处理这个文件三次,每次都很痛苦。我将不得不研究awk过滤器…也许我应该说文件是2.6Gb,不确定这是否真的重要,但我应该被定义得更详细。我只是尝试了一下,得到了相同的语法错误消息,但胡萝卜箭头出现在pd中的d下。我只是尝试了一下,得到了相同的语法错误消息,但胡萝卜箭头出现在pd中的d下。你能分享完整的代码列表吗?您收到SyntaxError,但不清楚原因。有一个完整的代码可以帮助揭示它我不100%确定我是如何做到这一点。我对Python非常陌生。原始文章中的行是我输入内容的复制粘贴,但我只是编辑了我的文章,以包含输入Python时Anaconda Powershell提示符中的所有文本。这就是你想要的吗?你能分享完整的代码清单吗?您收到SyntaxError,但不清楚原因。有一个完整的代码可以帮助揭示它我不100%确定我是如何做到这一点。我对Python非常陌生。原始文章中的行是我输入内容的复制粘贴,但我只是编辑了我的文章,以包含输入Python时Anaconda Powershell提示符中的所有文本。这就是你要找的吗?