No axis named 1 for object type class pandas.core.frame.DataFrame

2024/7/8 7:08:27

I created a DataFrame and I am trying to sort it based on the columns. I used the below code.

frame.sort_index(axis=1)

But this is causing the below errors

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-93c925b11670> in <module>()
----> 1 frame.sort_index(axis=Integer(1))/ext/sage/sage-8.4_1804/local/lib/python2.7/site-packages/pandas/core/frame.pyc in sort_index(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, by)4455                                     inplace=inplace)4456 
-> 4457         axis = self._get_axis_number(axis)4458         labels = self._get_axis(axis)4459 
/ext/sage/sage-8.4_1804/local/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_axis_number(self, axis)373                 pass374         raise ValueError('No axis named {0} for object type {1}'
--> 375                          .format(axis, type(self)))376 377     def _get_axis_name(self, axis):
ValueError: No axis named 1 for object type <class 'pandas.core.frame.DataFrame'>
Answer

For DataFrames, the value to be passed for axis is columns.

frame.sort_index(axis='columns')
https://en.xdnf.cn/q/120391.html

Related Q&A

str.replace with a variable

This is probably a simple fix, but having a little trouble getting my head around it; Im reading lines from a different script, and want to replace a line with a variable, however it replaces it with b…

How to generate DTD from XML?

Can a DTD be generated from an XML file using Python?

I have a very big list of dictionaries and I want to sum the insides

Something like{A: 3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847}, {B: 92374, 324, 345, 345, 45879, 34857987, 3457938457), {C: 23874923874987, 2347}How can I reduce that to {A: 2304923094820398},…

How to debug a TypeError no attribute __getitem__? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Change character based off of its position? Python 2.7

I have a string on unknown length, contain the characters a-z A-Z 0-9. I need to change each character using their position from Left to Right using a dictionary.Example:string = "aaaaaaaa" d…

Changing type using str() and int()...how it works

If I do this, I get:>>> x = 1 >>> y = 2 >>> type(x) <class int> >>> type(y) <class str>That all makes sense to me, except that if I convert using:>>…

Caesar cypher in Python

I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter w…

Flatten a list with sublists and strings [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)How do I make a flat list out of a list of lists?(32 answers)Closed 2 years ago.How can I flat…

Guitar string code in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

What is the point of initializing extensions with the flask instance in Flask? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…