简体   繁体   中英

python pandas - subsetting a dataframe with integer as column name

How to subset a dataframe with integer as column name?

I have this pandas dataframe:

df
-3  -2  -1  0  col1
--------------------
a    b   c  d  text1
e    f   g  h  text2

I can do this just fine:

print(df.col1)

But I cannot do this to subset:

print(df.0)

Even after I convert the column name to string from integer, I still cannot do this:

print(df.'0')

in R, to subset integer-based column name, we can do this

df$`0`

Need [] only for select:

print(df[0])

You can check attribute access :

You can use this access only if the index element is a valid python identifier, eg s.1 is not allowed. See here for an explanation of valid identifiers.

The attribute will not be available if it conflicts with an existing method name, eg s.min is not allowed.

Similarly, the attribute will not be available if it conflicts with any of the following list: index , major_axis , minor_axis , items , labels .

In any of these cases, standard indexing will still work, eg s['1'] , s['min'] , and s['index'] will access the corresponding element or column.

The Series/Panel accesses are available starting in 0.13.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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