import pandas as pd
columns = ['a', 'b', 'c'] # list of columns to be converted
converters = {
key: lambda x:
(
float(x.lower().split("e")[0].replace(",", ".")),
int(x.lower().split("e")[1])
) for key in columns
}
df = pd.read_csv(
"test.csv", # your source file
sep="\s+", # tabs and whitespace as separator
converters=converters,
)
将源文件test.csv与此
a b c
1,234e-4500 2,34e-3432 9,12e-123122
因此收益率
print df
print df['a'].dtype
a b c
0 (1.234, -4500) (2.34, -3432) (9.12, -123122)
object
如果将这两个值作为元组存储在DataFrame中足够方便,那么可以使用^{} 的转换函数来实现
将源文件
test.csv
与此因此收益率
旁注:正如弗拉基米尔指出的那样,如果问题允许这种解决方案,数据的标准化应该是一种首选方法
相关问题 更多 >
编程相关推荐