Search code examples
pythonpandasdataframedata-manipulationshift

Shift cells in a row of a Pandas DataFrame backwards


I have a pandas DataFrame below. I would like to shift the cells from "Undentified" back to "Info_1"

The following code is reproducible on your computer

df = pd.DataFrame({'Page': {2944: '12344'},
 'Pageviews': {2944: 1.0},
 'Unique Pageviews': {2944: 1.0},
 'Avg. Time on Page': {2944: '0:00:00'},
 'Entrances': {2944: 0.0},
 'Bounce Rate': {2944: '0.00%'},
 '% Exit': {2944: '0.00%'},
 0: {2944: ''},
 1: {2944: 'embed'},
 2: {2944: 'mywebsite'},
 3: {2944: 'product'},
 'ProductType': {2944: '10'},
 'ProductType': {2944: '12'},
 'ProductType': {2944: '15'},
 7: {2944: ''},
 'Info_1': {2944: ''},
 'Info_2': {2944: ''},
 'start': {2944: ''},
 'end': {2944: ''},
 'Age': {2944: ''},
 'Price': {2944: ''},
 14: {2944: ''},
 'Product_typeA': {2944: ''},
 16: {2944: ''},
 "undentified": {2944: '1'},
 18: {2944: '2'},
 19: {2944: '15918400'},
 20: {2944: '15514400'},
 21: {2944: '47'},
 22: {2944: '2200'},
 23: {2944: '15'},
 24: {2944: '3-4'},
 25: {2944: '2'},
 26: {2944: '4'},
 27: {2944: '1'},
 28: {2944: '2'},
 29: {2944: '1'},
 30: {2944: '113&a=5000'}})

Here is my attempt to shift it back:

df.loc["Undentified":].shift(-9,axis=1)

Unfortunately, this is not working. It does otherwise than expected. How do you recommend I tackle this?


Solution

  • You can do it using shift and transpose(.T)

    df2 = df.T.shift(-9, axis=0).T