1

I am trying to manipulate a Pandas DataFrame. See the Pandas DataFrame below: enter image description here

I am trying to shift from column 6 by a period of 1 if column 6 is missing.

Here is the idea:

tarrif_6_missing = df2[6].isnull()
df2[tarrif_6_missing] = df2[tarrif_6_missing].shift(1,axis=1)

The above is incorrect because it shifts the whole cells from the start of the columns. Instead if column 6 is missing. I would like to shift the columns by a period of 1 from the column 6 not from the start of the column.

EDIT: Thanks for the edits, but I am getting through, getting an error:

tarrif_6_missing = df2.loc[:,6].isnull()
df2.loc[:,tarrif_6_missing:] = df.loc[:,tarrif_6_missing:].shift(1, axis=1)

I get an error: TypeError: '0 False

And it does not go through

2
  • 1
    I'd be useful to also have an example of what you would like the result to look like. Does "shift by a period of 1" mean downwards or upwards, i.e., are you filling the holes or making them one longer? Commented Mar 5, 2020 at 16:07
  • 1
    Index goes first, then the columns, i.e. loc[tarrif_6_missing, column_6_name:] = ... Commented Mar 5, 2020 at 16:30

1 Answer 1

3

You can use .loc[] to act select a subpart of dataframe to shift like this:

 df.loc[:,column_index:]=df.loc[:,column_index:].shift(1, axis=1)
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting an error: df2[tarrif_6_missing] tarrif_6_missing = df2.loc[:,6].isnull() df2.loc[:,tarrif_6_missing:] = df.loc[:,tarrif_6_missing:].shift(1, axis=1)
df.loc[tarrif6missing,6]=df.loc[tarrif6missing,6].shift(1, axis=1) i think
Probably, irrelevant here, but once I got a weird error trying to shift horizontally, while df.T.shift(1).T worked perfectly. Just for your information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.