2
在下面的代码中,我将一个字符串读入DataFrame中,但即使输入字符串的标头是数字,它们也会作为字符串'1', '2'
读入。有没有办法以数字读取它们,或者将它们转换为数字?如何将DataFrame的列名从字符串转换为整数?
import pandas as pd
from StringIO import StringIO
string_input = " 1 2\n10 0.1 0.2\n20 0.1 0.2"
data = pd.read_table(StringIO(string_input), sep='\s+')
print data
print data.columns
1 2
10 0.1 0.2
20 0.1 0.2
Index([u'1', u'2'], dtype='object') # the columns names are of type str!!
你需要转换dtype:'data.columns = data.columns.astype(int) – EdChum