简体   繁体   English

通过名称附加python pandas multiindex数据框

[英]Appending by name with python pandas multiindex dataframe

I am tryining to append a pandas multiindex dataframe within a loop. 我正在尝试在循环内附加一个熊猫多索引数据框。

My multiindex dataframe looks a little like this (let's call it df1): 我的多索引数据帧看起来像这样(我们称它为df1):

method     (TestAa, TestAb)
properties level     l      r
0          0.0   144.6  161.4
1          0.1   146.3  161.4
2          0.2   148.0  161.4
3          0.3   149.7  161.4
4          0.4   151.3  161.4

Now I want to append another dataframe (df2) to it, so that the appended dataframe is sorted by the name "method" (which btw is a string tuple). 现在,我想向其附加另一个数据框(df2),以便按名称“方法”(btw是字符串元组)对附加的数据框进行排序。 In my mind it should look like that: 在我看来应该是这样的:

method     (TestAa, TestAb)     /  (TestBa, TestBb)                   
properties level       l      r /  level       l      r
0            0.0   144.6  161.4 /    0.0   150.6  161.4
1            0.1   146.3  161.4 /    0.1   151.3  161.4
2            0.2   148.0  161.4 /    0.2   152.0  161.4
3            0.3   149.7  161.4 /    0.3   153.7  161.4
4            0.4   151.3  161.4 /    0.4   155.3  161.4

In other words I want to add another column to an existing dataframe. 换句话说,我想向现有数据框添加另一列。

Right now I am creating the df as described in the doc : 现在,我正在按照文档中的说明创建df:

method = tuple([TestAa, TestAb])
columns = [[method]*3,["level", "l", "r"]]
tuples = list(zip(*columns))
index = pd.MultiIndex.from_tuples(tuples, names=['method', 'properties'])
df1= pd.DataFrame(columns=index, data=data, dtype=np.float)

Thank you in advance! 先感谢您!

我相信只需要concat

df = pd.concat([df1, df2], axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM