【发布时间】:2017-09-17 12:25:13
【问题描述】:
我有一个我认为是超级基本问题的问题,但我无法找到解决方案。简而言之,我在csv 中有一个列,它是一个数字列表。这个csv 是由pandas 和to_csv 生成的。当尝试使用read_csv 重新读取它时,它会自动将此数字列表转换为string。
然后尝试使用它时,我显然会遇到错误。当我尝试使用 to_numeric 函数时,我也会收到错误,因为它是一个列表,而不是单个数字。
有没有办法解决这个问题?在下面发布表单代码,但可能不是很有帮助:
def write_func(dataset):
features = featurize_list(dataset[column]) # Returns numpy array
new_dataset = dataset.copy() # Don't want to modify the underlying dataframe
new_dataset['Text'] = features
new_dataset.rename(columns={'Text': 'Features'}, inplace=True)
write(new_dataset, dataset_name)
def write(new_dataset, dataset_name):
dump_location = feature_set_location(dataset_name, self)
featurized_dataset.to_csv(dump_location)
def read_func(read_location):
df = pd.read_csv(read_location)
df['Features'] = df['Features'].apply(pd.to_numeric)
Features 列是有问题的列。当我尝试运行当前在 read_func 中的 apply 时,我收到此错误:
ValueError: Unable to parse string "[0.019636873200000002, 0.10695576670000001,...]" at position 0
我不可能是第一个遇到这个问题的人,有没有办法在读/写时处理这个问题?
【问题讨论】: