Pandas: DataFrame sorting [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
what is meaning of axis=1 in pandas sort_values function? [duplicate]
1 answer
What does axis in pandas mean?
13 answers
I'm trying to sort a dataframe using the sort_values method. I have a dataframe (df) with just two columns ('TS','VALUES') and I always getting a error as the follwoing:
df.columns
Out[88]: Index(['TS', 'VALUES'], dtype='object')
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
Traceback (most recent call last):
File "<ipython-input-89-53f79846b56c>", line 1, in <module>
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
File "C:UsersgianmAnaconda3libsite-packagespandascoreframe.py", line 4421, in sort_values
stacklevel=stacklevel)
File "C:UsersgianmAnaconda3libsite-packagespandascoregeneric.py", line 1382, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'TS'
It seems python does not recognize the name of the columns. Where I'am wrong ? Thanks
python pandas dataframe
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 15:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
what is meaning of axis=1 in pandas sort_values function? [duplicate]
1 answer
What does axis in pandas mean?
13 answers
I'm trying to sort a dataframe using the sort_values method. I have a dataframe (df) with just two columns ('TS','VALUES') and I always getting a error as the follwoing:
df.columns
Out[88]: Index(['TS', 'VALUES'], dtype='object')
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
Traceback (most recent call last):
File "<ipython-input-89-53f79846b56c>", line 1, in <module>
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
File "C:UsersgianmAnaconda3libsite-packagespandascoreframe.py", line 4421, in sort_values
stacklevel=stacklevel)
File "C:UsersgianmAnaconda3libsite-packagespandascoregeneric.py", line 1382, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'TS'
It seems python does not recognize the name of the columns. Where I'am wrong ? Thanks
python pandas dataframe
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 15:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
what is meaning of axis=1 in pandas sort_values function? [duplicate]
1 answer
What does axis in pandas mean?
13 answers
I'm trying to sort a dataframe using the sort_values method. I have a dataframe (df) with just two columns ('TS','VALUES') and I always getting a error as the follwoing:
df.columns
Out[88]: Index(['TS', 'VALUES'], dtype='object')
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
Traceback (most recent call last):
File "<ipython-input-89-53f79846b56c>", line 1, in <module>
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
File "C:UsersgianmAnaconda3libsite-packagespandascoreframe.py", line 4421, in sort_values
stacklevel=stacklevel)
File "C:UsersgianmAnaconda3libsite-packagespandascoregeneric.py", line 1382, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'TS'
It seems python does not recognize the name of the columns. Where I'am wrong ? Thanks
python pandas dataframe
This question already has an answer here:
what is meaning of axis=1 in pandas sort_values function? [duplicate]
1 answer
What does axis in pandas mean?
13 answers
I'm trying to sort a dataframe using the sort_values method. I have a dataframe (df) with just two columns ('TS','VALUES') and I always getting a error as the follwoing:
df.columns
Out[88]: Index(['TS', 'VALUES'], dtype='object')
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
Traceback (most recent call last):
File "<ipython-input-89-53f79846b56c>", line 1, in <module>
df.sort_values(by='TS',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
File "C:UsersgianmAnaconda3libsite-packagespandascoreframe.py", line 4421, in sort_values
stacklevel=stacklevel)
File "C:UsersgianmAnaconda3libsite-packagespandascoregeneric.py", line 1382, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'TS'
It seems python does not recognize the name of the columns. Where I'am wrong ? Thanks
This question already has an answer here:
what is meaning of axis=1 in pandas sort_values function? [duplicate]
1 answer
What does axis in pandas mean?
13 answers
python pandas dataframe
python pandas dataframe
edited Nov 20 at 14:51
jpp
89.2k195299
89.2k195299
asked Nov 20 at 14:49
polgia0
3819
3819
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 15:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 15:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Problem is axis=1
, it working for sorting by index values, so need axis=0
or remove it, because default parameter in sort_values
:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
df = pd.DataFrame({'TS': [40,1,4], 'a':[1,5,2], 'd':[7,8,9]}, index=list('abc'))
print (df)
TS a d
a 40 1 7
b 1 5 8
c 4 2 9
df.sort_values(by='TS',
axis=0,
ascending=True,
inplace=True,
kind='quicksort',
na_position='last')
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
If remove parameters with default values:
df.sort_values(by='TS', inplace=True)
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
df.sort_values(by='a',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
print (df)
a d TS
a 1 7 40
b 5 8 1
c 2 9 4
add a comment |
up vote
0
down vote
Is one of the 2 columns you're passing to sort_values your index?
If so you can use reset_index() to convert the index into a new column and then sort by both the column and the index.
data.reset_index().sort_values(['Column','Index']).set_index('Index')
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Problem is axis=1
, it working for sorting by index values, so need axis=0
or remove it, because default parameter in sort_values
:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
df = pd.DataFrame({'TS': [40,1,4], 'a':[1,5,2], 'd':[7,8,9]}, index=list('abc'))
print (df)
TS a d
a 40 1 7
b 1 5 8
c 4 2 9
df.sort_values(by='TS',
axis=0,
ascending=True,
inplace=True,
kind='quicksort',
na_position='last')
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
If remove parameters with default values:
df.sort_values(by='TS', inplace=True)
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
df.sort_values(by='a',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
print (df)
a d TS
a 1 7 40
b 5 8 1
c 2 9 4
add a comment |
up vote
1
down vote
Problem is axis=1
, it working for sorting by index values, so need axis=0
or remove it, because default parameter in sort_values
:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
df = pd.DataFrame({'TS': [40,1,4], 'a':[1,5,2], 'd':[7,8,9]}, index=list('abc'))
print (df)
TS a d
a 40 1 7
b 1 5 8
c 4 2 9
df.sort_values(by='TS',
axis=0,
ascending=True,
inplace=True,
kind='quicksort',
na_position='last')
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
If remove parameters with default values:
df.sort_values(by='TS', inplace=True)
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
df.sort_values(by='a',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
print (df)
a d TS
a 1 7 40
b 5 8 1
c 2 9 4
add a comment |
up vote
1
down vote
up vote
1
down vote
Problem is axis=1
, it working for sorting by index values, so need axis=0
or remove it, because default parameter in sort_values
:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
df = pd.DataFrame({'TS': [40,1,4], 'a':[1,5,2], 'd':[7,8,9]}, index=list('abc'))
print (df)
TS a d
a 40 1 7
b 1 5 8
c 4 2 9
df.sort_values(by='TS',
axis=0,
ascending=True,
inplace=True,
kind='quicksort',
na_position='last')
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
If remove parameters with default values:
df.sort_values(by='TS', inplace=True)
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
df.sort_values(by='a',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
print (df)
a d TS
a 1 7 40
b 5 8 1
c 2 9 4
Problem is axis=1
, it working for sorting by index values, so need axis=0
or remove it, because default parameter in sort_values
:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
df = pd.DataFrame({'TS': [40,1,4], 'a':[1,5,2], 'd':[7,8,9]}, index=list('abc'))
print (df)
TS a d
a 40 1 7
b 1 5 8
c 4 2 9
df.sort_values(by='TS',
axis=0,
ascending=True,
inplace=True,
kind='quicksort',
na_position='last')
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
If remove parameters with default values:
df.sort_values(by='TS', inplace=True)
print (df)
TS a d
b 1 5 8
c 4 2 9
a 40 1 7
df.sort_values(by='a',axis=1,ascending=True,inplace=True,kind='quicksort',na_position='last')
print (df)
a d TS
a 1 7 40
b 5 8 1
c 2 9 4
edited Nov 20 at 14:57
answered Nov 20 at 14:52
jezrael
317k22257335
317k22257335
add a comment |
add a comment |
up vote
0
down vote
Is one of the 2 columns you're passing to sort_values your index?
If so you can use reset_index() to convert the index into a new column and then sort by both the column and the index.
data.reset_index().sort_values(['Column','Index']).set_index('Index')
add a comment |
up vote
0
down vote
Is one of the 2 columns you're passing to sort_values your index?
If so you can use reset_index() to convert the index into a new column and then sort by both the column and the index.
data.reset_index().sort_values(['Column','Index']).set_index('Index')
add a comment |
up vote
0
down vote
up vote
0
down vote
Is one of the 2 columns you're passing to sort_values your index?
If so you can use reset_index() to convert the index into a new column and then sort by both the column and the index.
data.reset_index().sort_values(['Column','Index']).set_index('Index')
Is one of the 2 columns you're passing to sort_values your index?
If so you can use reset_index() to convert the index into a new column and then sort by both the column and the index.
data.reset_index().sort_values(['Column','Index']).set_index('Index')
answered Nov 20 at 14:57
philjet
253
253
add a comment |
add a comment |