我在网上找到一些代码,使用pandas按名称删除特定的列:
# import pandas with shortcut 'pd'
import pandas as pd
# read_csv function which is used to read the required CSV file
data = pd.read_csv('TradedInstrument_20230331_test.csv')
# drop function which is used in removing or deleting rows or columns from the CSV files
data.drop('isin', inplace=True, axis=1)
任何想法,如果这实际上是一个标签分隔的csv?
如果我对我的文件运行上面的代码,我得到一个UnicodeDecodeError:
Traceback (most recent call last):
....
File "pandas/_libs/parsers.pyx", line 1917, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 38835: invalid start byte
编码错误。它看起来像0xfc
是ü
字符(拉丁小写字母u与diaeresis),所以尝试使用encoding='latin1'
当你阅读你的csv文件:
data = pd.read_csv('TradedInstrument_20230331_test.csv', sep='t', encoding='latin1')
data.drop(columns='isin').to_csv('output.csv', index=False)