简体   繁体   English

如何将 DataFrame 的列名从字符串转换为整数

[英]How to convert column names of a DataFrame from string to integers

In the following code, I read a string into a DataFrame, but even though the headers of the input string are numbers, they are read in as strings '1', '2' .在下面的代码中,我将一个字符串读入 DataFrame,但即使输入字符串的标题是数字,它们也会作为字符串'1', '2'读入。 Is there a way to read them in as numbers, or convert them to numbers afterwards?有没有办法将它们读取为数字,或者之后将它们转换为数字?

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!!

You can do this as a post-processing step using astype(int) :您可以使用astype(int)作为后处理步骤来执行此操作:

In [86]:
string_input = " 1 2\n10 0.1 0.2\n20 0.1 0.2"
data = pd.read_table(io.StringIO(string_input), sep='\s+')
print (data)
print (data.columns.astype(int))
​
      1    2
10  0.1  0.2
20  0.1  0.2
Int64Index([1, 2], dtype='int64')

personally I would prefer string columns as it becomes less ambiguous when indexing IMO when reading and writing code, as in doing df['col_name'] becomes a habit and when you have a default int64 index then df.loc[some_int] is unambiguous我个人更喜欢字符串列,因为在读取和编写代码时索引 IMO 时它变得不那么模棱两可了,就像做df['col_name']成为一种习惯,当你有一个默认的int64索引时, df.loc[some_int]是明确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM