Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:18:42+00:00 2026-06-13T15:18:42+00:00

I would like to modify DataFrame entries for some rows at a specific level

  • 0

I would like to modify DataFrame entries for some rows at a specific level of a hierarchical index. Here is a canonical example:

>>> index = pd.MultiIndex.from_arrays([['a','a', 'b', 'b'], [1,2,1,2]], 
...                                   names=['first', 'second'])
>>> data = pd.DataFrame(np.random.rand(len(index)), index=index, columns=['A'])
>>> print data
                     A
first second          
a     1       0.587781
      2       0.560407
b     1       0.492996
      2       0.267799

I would like to set rows for which second==2 to 0 (for example). I tried using DataFrame.xs method but it returns a copy and not a view:

>>> selected = data.xs(2, level='second')
>>> print selected
              A
first          
a      0.560407
b      0.267799

>>> selected['A']=0
>>> print data

                    A
first second          
a     1       0.587781
      2       0.560407
b     1       0.492996
      2       0.267799

The last assignment did not affect data (it changed values in selected of course).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:18:43+00:00Added an answer on June 13, 2026 at 3:18 pm

    In recent version of pandas help(data.xs) shows how to get a view on the data.

    It can be done by using xs arg copy=False.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to modify qemu source code to get some lower level information
I would like to modify an object private variable class Example(): __myTest1 = 1
I would like to transform/modify the content of dataframe. Basically I have a dataframe
I would like to modify the legend in ggplot2 using the text geom. Example
I would like to modify some database data as part of an alembic upgrade.
I have a String which I would like to modify in some way. For
I would like modify HTML like I am <b>Sadi, novice</b> programmer. to I am
We have a customer that would like to modify application user messages that we
I'm absolutely new to JavaScript and would like to modify a textarea of a
I'm using HighLine to write my console application and I would like to modify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.