0

So, I split the sentences in a row into single words, and thus, the rows of my dataframe got lengthened. however, I am not satisfied with the new indexes.

0  0                I
   1              don
   2                '
   3                t
   4            think
   5              any

What i would like, would be to make the index on the left swap places with the next one, and then, pass a argument like (Sentence: 'X'), where X would denote the original index of the sentence which the word belonged to.

 0      Sentence:0          I
 1      Sentence:0          don
 2      Sentence:0          '
 3      Sentence:0          t
 4      Sentence:0          think
 5                          any  

However,

df['index1'] = df.index

only lets me access the second index, and so i cannot get the indexes to swap positions.

How might I go about this?

1 Answer 1

1

Do I understand right that you want to swap the levels of your Multiindex?

Maybe this helps you:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.swaplevel.html

Edit: Answering the second part of your question:

df.rename(lambda x: "Sentence "+str(x), level=1)

does the renaming you asked for

Sign up to request clarification or add additional context in comments.

Comments

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.