Shifting multi-column data to the left Pandas Dataframe [duplicate]
This question already has an answer here:
Left justify string values in a pandas DataFrame
1 answer
I'd like to make whole columns data moved to left.
Only data should be shifted to the left 1 time and the columns should be fixed.
Could you tell me how can I do this?
Initial Data is :
A1 A2 A3 A4 A5
#0001 421 000 000 777 888
#0002 382 403 430 320 055
#0003 441 304 403 403 403
#0004 430 403 206 N/A 312
#0005 N/A 394 493 N/A 403
Desire Data is :
A1 A2 A3 A4 A5
#0001 000 000 777 888 N/A
#0002 403 430 320 055 N/A
#0003 304 403 403 403 N/A
#0004 403 206 N/A 312 N/A
#0005 394 493 N/A 403 N/A
python pandas
marked as duplicate by coldspeed
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 15 '18 at 5:32
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 |
This question already has an answer here:
Left justify string values in a pandas DataFrame
1 answer
I'd like to make whole columns data moved to left.
Only data should be shifted to the left 1 time and the columns should be fixed.
Could you tell me how can I do this?
Initial Data is :
A1 A2 A3 A4 A5
#0001 421 000 000 777 888
#0002 382 403 430 320 055
#0003 441 304 403 403 403
#0004 430 403 206 N/A 312
#0005 N/A 394 493 N/A 403
Desire Data is :
A1 A2 A3 A4 A5
#0001 000 000 777 888 N/A
#0002 403 430 320 055 N/A
#0003 304 403 403 403 N/A
#0004 403 206 N/A 312 N/A
#0005 394 493 N/A 403 N/A
python pandas
marked as duplicate by coldspeed
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 15 '18 at 5:32
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.
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
@coldspeed Can you please write a comment on how to usejustify
to this scenario. To learn how it works before deleting my answer.
– Sandeep Kadapa
Nov 15 '18 at 5:59
@SandeepKadapa If they are numbers, you can simply pass an array tojustify
:v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@coldspeed Checked isn't giving right results also the output array contains extreme values-2147483648
in place ofnp.nan
. Could you verify it?.
– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38
add a comment |
This question already has an answer here:
Left justify string values in a pandas DataFrame
1 answer
I'd like to make whole columns data moved to left.
Only data should be shifted to the left 1 time and the columns should be fixed.
Could you tell me how can I do this?
Initial Data is :
A1 A2 A3 A4 A5
#0001 421 000 000 777 888
#0002 382 403 430 320 055
#0003 441 304 403 403 403
#0004 430 403 206 N/A 312
#0005 N/A 394 493 N/A 403
Desire Data is :
A1 A2 A3 A4 A5
#0001 000 000 777 888 N/A
#0002 403 430 320 055 N/A
#0003 304 403 403 403 N/A
#0004 403 206 N/A 312 N/A
#0005 394 493 N/A 403 N/A
python pandas
This question already has an answer here:
Left justify string values in a pandas DataFrame
1 answer
I'd like to make whole columns data moved to left.
Only data should be shifted to the left 1 time and the columns should be fixed.
Could you tell me how can I do this?
Initial Data is :
A1 A2 A3 A4 A5
#0001 421 000 000 777 888
#0002 382 403 430 320 055
#0003 441 304 403 403 403
#0004 430 403 206 N/A 312
#0005 N/A 394 493 N/A 403
Desire Data is :
A1 A2 A3 A4 A5
#0001 000 000 777 888 N/A
#0002 403 430 320 055 N/A
#0003 304 403 403 403 N/A
#0004 403 206 N/A 312 N/A
#0005 394 493 N/A 403 N/A
This question already has an answer here:
Left justify string values in a pandas DataFrame
1 answer
python pandas
python pandas
asked Nov 15 '18 at 4:41
Landon NamLandon Nam
212
212
marked as duplicate by coldspeed
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 15 '18 at 5:32
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 coldspeed
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 15 '18 at 5:32
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.
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
@coldspeed Can you please write a comment on how to usejustify
to this scenario. To learn how it works before deleting my answer.
– Sandeep Kadapa
Nov 15 '18 at 5:59
@SandeepKadapa If they are numbers, you can simply pass an array tojustify
:v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@coldspeed Checked isn't giving right results also the output array contains extreme values-2147483648
in place ofnp.nan
. Could you verify it?.
– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38
add a comment |
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
@coldspeed Can you please write a comment on how to usejustify
to this scenario. To learn how it works before deleting my answer.
– Sandeep Kadapa
Nov 15 '18 at 5:59
@SandeepKadapa If they are numbers, you can simply pass an array tojustify
:v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@coldspeed Checked isn't giving right results also the output array contains extreme values-2147483648
in place ofnp.nan
. Could you verify it?.
– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
@coldspeed Can you please write a comment on how to use
justify
to this scenario. To learn how it works before deleting my answer.– Sandeep Kadapa
Nov 15 '18 at 5:59
@coldspeed Can you please write a comment on how to use
justify
to this scenario. To learn how it works before deleting my answer.– Sandeep Kadapa
Nov 15 '18 at 5:59
@SandeepKadapa If they are numbers, you can simply pass an array to
justify
: v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@SandeepKadapa If they are numbers, you can simply pass an array to
justify
: v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@coldspeed Checked isn't giving right results also the output array contains extreme values
-2147483648
in place of np.nan
. Could you verify it?.– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed Checked isn't giving right results also the output array contains extreme values
-2147483648
in place of np.nan
. Could you verify it?.– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38
add a comment |
2 Answers
2
active
oldest
votes
Use double transpose with shift
:
df = df.T.shift(-1,axis=0).T
print(df)
A1 A2 A3 A4 A5
#0001 0.0 0.0 777.0 888.0 NaN
#0002 403.0 430.0 320.0 55.0 NaN
#0003 304.0 403.0 403.0 403.0 NaN
#0004 403.0 206.0 NaN 312.0 NaN
#0005 394.0 493.0 NaN 403.0 NaN
Actually df.shift(-1,axis=1)
should work but there is bug in the code:
print(df.shift(-1,axis=1))
A1 A2 A3 A4 A5
#0001 777.0 0.0 888.0 NaN NaN
#0002 320.0 430.0 55.0 NaN NaN
#0003 403.0 403.0 403.0 NaN NaN
#0004 NaN 206.0 312.0 NaN NaN
#0005 NaN 493.0 403.0 NaN NaN
1
Simplydf.shift(-1, axis=1)
shifts on column.
– rushinstuffin
Nov 15 '18 at 4:55
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
add a comment |
Try this simple loop:
for i in range(len(df.columns)-1):
df.iloc[:,i] = df.iloc[:,i+1]
# for last column:
df[i+1] = np.nan
Output:
A1 A2 A3 A4 A5
#0001 0 0 777.0 888 NaN
#0002 403 430 320.0 55 NaN
#0003 304 403 403.0 403 NaN
#0004 403 206 NaN 312 NaN
#0005 394 493 NaN 403 NaN
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use double transpose with shift
:
df = df.T.shift(-1,axis=0).T
print(df)
A1 A2 A3 A4 A5
#0001 0.0 0.0 777.0 888.0 NaN
#0002 403.0 430.0 320.0 55.0 NaN
#0003 304.0 403.0 403.0 403.0 NaN
#0004 403.0 206.0 NaN 312.0 NaN
#0005 394.0 493.0 NaN 403.0 NaN
Actually df.shift(-1,axis=1)
should work but there is bug in the code:
print(df.shift(-1,axis=1))
A1 A2 A3 A4 A5
#0001 777.0 0.0 888.0 NaN NaN
#0002 320.0 430.0 55.0 NaN NaN
#0003 403.0 403.0 403.0 NaN NaN
#0004 NaN 206.0 312.0 NaN NaN
#0005 NaN 493.0 403.0 NaN NaN
1
Simplydf.shift(-1, axis=1)
shifts on column.
– rushinstuffin
Nov 15 '18 at 4:55
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
add a comment |
Use double transpose with shift
:
df = df.T.shift(-1,axis=0).T
print(df)
A1 A2 A3 A4 A5
#0001 0.0 0.0 777.0 888.0 NaN
#0002 403.0 430.0 320.0 55.0 NaN
#0003 304.0 403.0 403.0 403.0 NaN
#0004 403.0 206.0 NaN 312.0 NaN
#0005 394.0 493.0 NaN 403.0 NaN
Actually df.shift(-1,axis=1)
should work but there is bug in the code:
print(df.shift(-1,axis=1))
A1 A2 A3 A4 A5
#0001 777.0 0.0 888.0 NaN NaN
#0002 320.0 430.0 55.0 NaN NaN
#0003 403.0 403.0 403.0 NaN NaN
#0004 NaN 206.0 312.0 NaN NaN
#0005 NaN 493.0 403.0 NaN NaN
1
Simplydf.shift(-1, axis=1)
shifts on column.
– rushinstuffin
Nov 15 '18 at 4:55
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
add a comment |
Use double transpose with shift
:
df = df.T.shift(-1,axis=0).T
print(df)
A1 A2 A3 A4 A5
#0001 0.0 0.0 777.0 888.0 NaN
#0002 403.0 430.0 320.0 55.0 NaN
#0003 304.0 403.0 403.0 403.0 NaN
#0004 403.0 206.0 NaN 312.0 NaN
#0005 394.0 493.0 NaN 403.0 NaN
Actually df.shift(-1,axis=1)
should work but there is bug in the code:
print(df.shift(-1,axis=1))
A1 A2 A3 A4 A5
#0001 777.0 0.0 888.0 NaN NaN
#0002 320.0 430.0 55.0 NaN NaN
#0003 403.0 403.0 403.0 NaN NaN
#0004 NaN 206.0 312.0 NaN NaN
#0005 NaN 493.0 403.0 NaN NaN
Use double transpose with shift
:
df = df.T.shift(-1,axis=0).T
print(df)
A1 A2 A3 A4 A5
#0001 0.0 0.0 777.0 888.0 NaN
#0002 403.0 430.0 320.0 55.0 NaN
#0003 304.0 403.0 403.0 403.0 NaN
#0004 403.0 206.0 NaN 312.0 NaN
#0005 394.0 493.0 NaN 403.0 NaN
Actually df.shift(-1,axis=1)
should work but there is bug in the code:
print(df.shift(-1,axis=1))
A1 A2 A3 A4 A5
#0001 777.0 0.0 888.0 NaN NaN
#0002 320.0 430.0 55.0 NaN NaN
#0003 403.0 403.0 403.0 NaN NaN
#0004 NaN 206.0 312.0 NaN NaN
#0005 NaN 493.0 403.0 NaN NaN
edited Nov 15 '18 at 4:56
answered Nov 15 '18 at 4:46
Sandeep KadapaSandeep Kadapa
7,218830
7,218830
1
Simplydf.shift(-1, axis=1)
shifts on column.
– rushinstuffin
Nov 15 '18 at 4:55
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
add a comment |
1
Simplydf.shift(-1, axis=1)
shifts on column.
– rushinstuffin
Nov 15 '18 at 4:55
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
1
1
Simply
df.shift(-1, axis=1)
shifts on column.– rushinstuffin
Nov 15 '18 at 4:55
Simply
df.shift(-1, axis=1)
shifts on column.– rushinstuffin
Nov 15 '18 at 4:55
1
1
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
@rushinstuffin It doesn't work, there is bug in shift code.
– Sandeep Kadapa
Nov 15 '18 at 4:57
1
1
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
Oh, thanks. I didn't realize that. It seems to be because of the NaN values? Without those it seems to work fine.
– rushinstuffin
Nov 15 '18 at 5:06
add a comment |
Try this simple loop:
for i in range(len(df.columns)-1):
df.iloc[:,i] = df.iloc[:,i+1]
# for last column:
df[i+1] = np.nan
Output:
A1 A2 A3 A4 A5
#0001 0 0 777.0 888 NaN
#0002 403 430 320.0 55 NaN
#0003 304 403 403.0 403 NaN
#0004 403 206 NaN 312 NaN
#0005 394 493 NaN 403 NaN
add a comment |
Try this simple loop:
for i in range(len(df.columns)-1):
df.iloc[:,i] = df.iloc[:,i+1]
# for last column:
df[i+1] = np.nan
Output:
A1 A2 A3 A4 A5
#0001 0 0 777.0 888 NaN
#0002 403 430 320.0 55 NaN
#0003 304 403 403.0 403 NaN
#0004 403 206 NaN 312 NaN
#0005 394 493 NaN 403 NaN
add a comment |
Try this simple loop:
for i in range(len(df.columns)-1):
df.iloc[:,i] = df.iloc[:,i+1]
# for last column:
df[i+1] = np.nan
Output:
A1 A2 A3 A4 A5
#0001 0 0 777.0 888 NaN
#0002 403 430 320.0 55 NaN
#0003 304 403 403.0 403 NaN
#0004 403 206 NaN 312 NaN
#0005 394 493 NaN 403 NaN
Try this simple loop:
for i in range(len(df.columns)-1):
df.iloc[:,i] = df.iloc[:,i+1]
# for last column:
df[i+1] = np.nan
Output:
A1 A2 A3 A4 A5
#0001 0 0 777.0 888 NaN
#0002 403 430 320.0 55 NaN
#0003 304 403 403.0 403 NaN
#0004 403 206 NaN 312 NaN
#0005 394 493 NaN 403 NaN
edited Nov 15 '18 at 6:41
answered Nov 15 '18 at 4:56
rnsornso
11.9k1441101
11.9k1441101
add a comment |
add a comment |
stackoverflow.com/a/47898659/4909087 works for numeric data, check the link.
– coldspeed
Nov 15 '18 at 5:32
@coldspeed Can you please write a comment on how to use
justify
to this scenario. To learn how it works before deleting my answer.– Sandeep Kadapa
Nov 15 '18 at 5:59
@SandeepKadapa If they are numbers, you can simply pass an array to
justify
:v = df.values; v = justify(v); df.values[:] = v
– coldspeed
Nov 15 '18 at 6:02
@coldspeed Checked isn't giving right results also the output array contains extreme values
-2147483648
in place ofnp.nan
. Could you verify it?.– Sandeep Kadapa
Nov 15 '18 at 6:10
@coldspeed : that question is quite different. This question can be solved by much simpler answers.
– rnso
Nov 15 '18 at 6:38