在使用pandas库时,我想读取数据并将其写入csv文件。使用to_csv将DataFrame写入csv文件,一切都很好。当我尝试将值读回python解释器时,出现了我的问题。
参数index_col = None不会更改输出。
#Pass some keys and values to a pandas DataFrame held in variable df
df = pd.DataFrame({'Artist':['Sublime','Blink 182','Nirvana'],
'Album':['Sublime','Blink 182','Nevermind'],
'Hit Single':["What I've Got", 'All the Small Things',
'Smells Like Teen Spirit']})
#Print DataFrame
df
#Write the data to a spreadsheet(comma separated value file type)
df.to_csv('filename.csv')
#Read the values back into the df varaible
df =pd.read_csv('filename.csv')
#Print out values in df variable
df
使用read_csv读回数据后,第二列的顶部将出现Unnamed :,以及一组额外的从0到2 0递增的数字索引出现两次。如何摆脱多余的多余列?
如果您需要阅读index
读取的文件
df = pd.read_csv("filename.csv", index_col=0)
如果不这样做,请保存
df.to_csv('filename.csv', index=False)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句