Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何向DataFrame中添加序列,其中包含自定义索引_Python_Pandas - Fatal编程技术网

Python 如何向DataFrame中添加序列,其中包含自定义索引

Python 如何向DataFrame中添加序列,其中包含自定义索引,python,pandas,Python,Pandas,我有以下数据帧: purchase_1 = pd.Series({'Name': 'Chris', 'Item Purchased': 'Dog Food', 'Cost': 22.50}) purchase_2 = pd.Series({'Name': 'Kevyn', 'Item Purchased': 'Kitty Litter',

我有以下数据帧:

purchase_1 = pd.Series({'Name': 'Chris',
                        'Item Purchased': 'Dog Food',
                        'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
                        'Item Purchased': 'Kitty Litter',
                        'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
                        'Item Purchased': 'Bird Seed',
                        'Cost': 5.00})
df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])
df['Location'] = df.index
df = df.set_index(['Location', 'Name'])
df2 = df.copy()
print(df2)

                Cost Item Purchased
Location Name                      
Store 1  Chris  22.5       Dog Food
         Kevyn   2.5   Kitty Litter
Store 2  Vinod   5.0      Bird Seed
然后我有以下系列:

purchase_4 = pd.Series({'Name': 'Kevyn',
                        'Item Purchased': 'Kitty Food',
                        'Cost': 3.00,
                        'Location': 'Store 2'})
当我尝试将此系列添加到我的DF时,它可以工作,但有一堆NAN:

df2 = df2.append(purchase_4, ignore_index=True)

   Cost Item Purchased Location   Name
0  22.5       Dog Food      NaN    NaN
1   2.5   Kitty Litter      NaN    NaN
2   5.0      Bird Seed      NaN    NaN
3   3.0     Kitty Food  Store 2  Kevyn
您可以使用:

或者,对于集合多索引,请使用
()

替代解决方案:

In [237]: df.append(purchase_4.to_frame().T.set_index(df.index.names))
Out[237]:
                Cost Item Purchased
Location Name
Store 1  Chris  22.5       Dog Food
         Kevyn   2.5   Kitty Litter
Store 2  Vinod     5      Bird Seed
         Kevyn     3     Kitty Food
df=df.set_索引([df.index,'Name'])) df.index.names=['Location','Name'] df=df.append(pd.Series(数据={'Cost':3.00,'Item Purchased':'Kitty Food'),name=('Store 2','Kevyn'))
df

这太短了。请详细说明这是如何解决本页顶部问题中描述的问题的。
df2.loc[(purchase_4['Location'], purchase_4['Name']),:] = purchase_4
print (df2)
                Cost Item Purchased
Location Name                      
Store 1  Chris  22.5       Dog Food
         Kevyn   2.5   Kitty Litter
Store 2  Vinod   5.0      Bird Seed
         Kevyn   3.0     Kitty Food
In [237]: df.append(purchase_4.to_frame().T.set_index(df.index.names))
Out[237]:
                Cost Item Purchased
Location Name
Store 1  Chris  22.5       Dog Food
         Kevyn   2.5   Kitty Litter
Store 2  Vinod     5      Bird Seed
         Kevyn     3     Kitty Food