我正在编写代码以在csv文件中插入新列:
import sys,os,csv,glob
dir = os.path.dirname(__file__)
import pandas as pd
updatecsv()
def updatecsv():
files = 'example.cs'
df = pd.read_csv(files)
df = df.convert_objects(convert_numeric=True)
#until here, the code is running fine
#now i wanted to add a new column in a specific index with all value =10
df.insert(2,'new',1000)
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,没有给出错误.当我打开csv文件时,不会添加新行.我决定使用python shell检查:
>>>files = 'example.csv'
>>>df = pd.read_csv(files)
>>>df = df.convert_objects(convert_numeric=True)
>>>df
A B C D
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
df['new']=13
>>>df
A …Run Code Online (Sandbox Code Playgroud) 我正在研究大型文档中的tf-idf.我的单词数量超过80,000.我试图在csv文件中写稀疏矩阵.我使用的代码与此处的答案类似如何使用Python将新列添加到CSV文件中?
输出文件太大,超过700 MB,仅约30,000个单词.那么,我的问题是如何有效地编写它?谢谢.