Update pandas DataFrame Multilevel Index with a Series?

Ramón J Romero y Vigil

Given a DataFrame with a multi-level index (in this example 'userId' & 'date' are part of the index):

                 clicks
userId   date
x        1/1     1
         1/2     2
y        1/1     3
         1/3     4
z        1/1     4

And given a Series that has values from the first level of the DataFrame's multi-index as its own index:

x    Alice
z    Charlie
y    Bob

How can the first level of the DataFrame index be updated to be the values from a Series? The resulting DataFrame would look like

Alice    1/1     1
         1/2     2
Bob      1/1     3
         1/3     4
Charlie  1/1     4

Note: I cannot rely on the ordering of the Series being the same ordering as the first level of the multi-index (e.g. the Series index is ['x', 'z', 'y'] while the DataFrame first-level is ['x', 'y', 'z'])

Thank you in advance for your consideration and response.

ALollz

Map the level using the Series, then assign the index back.

import pandas as pd

df.index = pd.MultiIndex.from_arrays(
            [df.index.get_level_values('userId').map(s),  # s is your Series
             df.index.get_level_values('date')])

             clicks
userId  date        
Alice   1/1        1
        1/2        2
Bob     1/1        3
        1/3        4
Charlie 1/1        4

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Python: pivot a pandas DataFrame when the desired index Series has duplicates

분류에서Dev

시리즈로 Pandas DataFrame Multilevel Index를 업데이트 하시겠습니까?

분류에서Dev

Updated dataframe - keep the last update - dataframe pandas

분류에서Dev

In Python, how can I update multiple rows in a DataFrame with a Series?

분류에서Dev

How to reindex a multi-index pandas dataframe?

분류에서Dev

Index Pandas에 DataFrame 추가

분류에서Dev

python/pandas: update a column based on a series holding sums of that same column

분류에서Dev

Creating new pandas DataFrame from existing DataFrame and index

분류에서Dev

How to plot beautifully the segmentation of time series (pandas dataframe)

분류에서Dev

how to use values of a pandas DataFrame as numpy array index

분류에서Dev

Python Pandas Setting Dataframe index and Column names from an array

분류에서Dev

Update the values of a dictionary checking if there are new values on a column of a Pandas Dataframe

분류에서Dev

pd.reset_index가 Series를 DataFrame으로 변환하는 이유

분류에서Dev

Python DataFrame은 pandas.core.series.Series 유형의 두 열을 단일 열로 결합

분류에서Dev

일련의 인덱스로 Pandas DataFrame / Series 인덱싱

분류에서Dev

Pandas는 Series로 DataFrame을 필터링합니다.

분류에서Dev

Pandas Series 및 Dataframe에서 Spacy를 사용하는 lemmetization 문제

분류에서Dev

Pandas는 열을 통해 DataFrame 및 Series를 결합합니다.

분류에서Dev

Pandas Series / Dataframe에 그룹별로 그룹화

분류에서Dev

DataFrame의 Python MultiLevel Index. 첫 번째 인덱스 수준의 첫 번째 행에 액세스하여 기능을 적용합니다.

분류에서Dev

Resample Time Series in pandas

분류에서Dev

Convert pandas series into integers

분류에서Dev

Pandas DataFrame

분류에서Dev

Index를 사용하여 Pandas Dataframe 열에 대한 작업

분류에서Dev

Pass a pd.Series to a dataframe?

분류에서Dev

MultiIndex 레이블이있는 Pandas DataFrame.update

분류에서Dev

Pandas : pandas.DataFrame.update에 의해 업데이트 된 셀 가져 오기

분류에서Dev

Pandas - Extracting data from Series

분류에서Dev

Time Series using numpy or pandas

Related 관련 기사

  1. 1

    Python: pivot a pandas DataFrame when the desired index Series has duplicates

  2. 2

    시리즈로 Pandas DataFrame Multilevel Index를 업데이트 하시겠습니까?

  3. 3

    Updated dataframe - keep the last update - dataframe pandas

  4. 4

    In Python, how can I update multiple rows in a DataFrame with a Series?

  5. 5

    How to reindex a multi-index pandas dataframe?

  6. 6

    Index Pandas에 DataFrame 추가

  7. 7

    python/pandas: update a column based on a series holding sums of that same column

  8. 8

    Creating new pandas DataFrame from existing DataFrame and index

  9. 9

    How to plot beautifully the segmentation of time series (pandas dataframe)

  10. 10

    how to use values of a pandas DataFrame as numpy array index

  11. 11

    Python Pandas Setting Dataframe index and Column names from an array

  12. 12

    Update the values of a dictionary checking if there are new values on a column of a Pandas Dataframe

  13. 13

    pd.reset_index가 Series를 DataFrame으로 변환하는 이유

  14. 14

    Python DataFrame은 pandas.core.series.Series 유형의 두 열을 단일 열로 결합

  15. 15

    일련의 인덱스로 Pandas DataFrame / Series 인덱싱

  16. 16

    Pandas는 Series로 DataFrame을 필터링합니다.

  17. 17

    Pandas Series 및 Dataframe에서 Spacy를 사용하는 lemmetization 문제

  18. 18

    Pandas는 열을 통해 DataFrame 및 Series를 결합합니다.

  19. 19

    Pandas Series / Dataframe에 그룹별로 그룹화

  20. 20

    DataFrame의 Python MultiLevel Index. 첫 번째 인덱스 수준의 첫 번째 행에 액세스하여 기능을 적용합니다.

  21. 21

    Resample Time Series in pandas

  22. 22

    Convert pandas series into integers

  23. 23

    Pandas DataFrame

  24. 24

    Index를 사용하여 Pandas Dataframe 열에 대한 작업

  25. 25

    Pass a pd.Series to a dataframe?

  26. 26

    MultiIndex 레이블이있는 Pandas DataFrame.update

  27. 27

    Pandas : pandas.DataFrame.update에 의해 업데이트 된 셀 가져 오기

  28. 28

    Pandas - Extracting data from Series

  29. 29

    Time Series using numpy or pandas

뜨겁다태그

보관