Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pd.DataFrame中插入一行而不加载文件_Python_Python 3.x_Pandas - Fatal编程技术网

Python 在pd.DataFrame中插入一行而不加载文件

Python 在pd.DataFrame中插入一行而不加载文件,python,python-3.x,pandas,Python,Python 3.x,Pandas,以下代码可以有效地将一行(要素名称)作为第一行插入我的数据集中: features = ['VendorID', 'mta_tax', 'tip_amount', 'tolls_amount', 'improvement_surcharge', 'total_amount'] df = pd.DataFrame(pd.read_csv(path + 'data.csv', sep=',')) df.loc[-1] = features # adding a row df.index = df

以下代码可以有效地将一行(要素名称)作为第一行插入我的数据集中:

features = ['VendorID', 'mta_tax', 'tip_amount', 'tolls_amount', 'improvement_surcharge', 'total_amount']

df = pd.DataFrame(pd.read_csv(path + 'data.csv', sep=','))
df.loc[-1] = features  # adding a row
df.index = df.index + 1  # shifting index
df = df.sort_index()  # sorting by index
但是
data.csv
非常大~10 GB,因此我想知道是否可以在文件中直接插入
功能
行而不加载它!可能吗


谢谢

您不必将整个文件加载到内存中,请使用stdlib
csv
模块的
writer
功能在文件末尾追加一行

import csv
import os

with open(os.path.join(path, 'data.csv'), 'a') as f:
    writer = csv.writer(f)
    writer.writerow(features)

@Ste。如果不移动所有现有的10GB数据,这是非常困难的。我明白了。非常感谢您的支持post@Ste. 这里是关于这个主题的,几乎所有的答案都涉及到移动数据。祝你好运