Pandas multiple index dataframe: creating new index or appending to existing index

浪尽此生 提交于 2019-12-11 06:30:01

问题


I have a Pandas dataframe, multi_df, which has a multi-index made of the code,colour,texture and shape values as below:

import pandas as pd
import numpy as np
df = pd.DataFrame({'id' : range(1,9),
                    'code' : ['one', 'one', 'two', 'three',
                                'two', 'three', 'one', 'two'],
                    'colour': ['black', 'white','white','white',
                            'black', 'black', 'white', 'white'],
                    'texture': ['soft', 'soft', 'hard','soft','hard',
                                        'hard','hard','hard'],
                    'shape': ['round', 'triangular', 'triangular','triangular','square',
                                        'triangular','round','triangular'],
                    'amount' : np.random.randn(8)},  columns= ['id','code','colour', 'texture', 'shape', 'amount'])
multi_df = df.set_index(['code','colour','texture','shape']).sort_index()['id']
multi_df
code   colour  texture  shape     
one    black   soft     round         1
       white   hard     round         7
               soft     triangular    2
three  black   hard     triangular    6
       white   soft     triangular    4
two    black   hard     square        5
       white   hard     triangular    3
                        triangular    8
Name: id, dtype: int64

I am given a new index - new_id couple. If the new_index (combination) already exists in the multi_df, I want to append the new_id to the existing index. If the new_index does not exist, I want to create it and add the id value. For instance:

new_id = 15
new_index = ('two','white','hard', 'triangular')
if new_index in multi_df.index:
    # APPEND TO EXISTING: multi_df[('two','white','hard', 'triangular')].append(new_id)
else:
    # CREATE NEW index and put the new_id in.

However, I cannot figure out the syntax for appending (IF) or creating (ELSE) the new index. Any help would be most welcome.

P.S: for appending I can see that the object that I am trying to add the new_id to is a Series. However, append() does not work..

type(multi_df[('two','white','hard', 'triangular')])
<class 'pandas.core.series.Series'>

回答1:


append() creates a new series every time, so it's very slow, if you need call this in a for loop:

data = pd.Series(15, index=pd.MultiIndex.from_tuples([('two','white','hard', 'triangular')]))
multi_df.append(data)


来源:https://stackoverflow.com/questions/21253580/pandas-multiple-index-dataframe-creating-new-index-or-appending-to-existing-ind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!