简体   繁体   English

熊猫数据框:如何定义几列属性?

[英]Pandas dataframe : how to define several columns attribute ?

My question is so general that I'm pretty sure it might be a duplicate but I couldn't find the answer... For this, I apologize in advance ! 我的问题是如此笼统,以至于我敢肯定它可能是重复的,但是我找不到答案...为此,我事先表示歉意!

I have the following issue : 我有以下问题:

Let's consider the pandas dataset obtained with the following lines: 让我们考虑通过以下几行获得的熊猫数据集:

import numpy as np
import pandas as pd

dates = pd.date_range( '20130101', periods=6 )
df = pd.DataFrame( np.random.randn( 6, 4 ), index=dates, columns=list( 'ABCD' ) )
df2 = pd.DataFrame ( [["dog", "dog", "snake", "cat"],["mammel", "mammel", "reptilian", "mammel"]], columns=list('ABCD'))
df2 = df2.append(df)

Which gives the following structure : 给出以下结构:

                            A         B          C          D
0                         dog       dog      snake        cat
1                      mammel    mammel  reptilian     mammel
2013-01-01 00:00:00  0.876641  -1.23665   0.375619  -0.642984
2013-01-02 00:00:00  -1.10135  0.264016  0.0893244     1.7381
2013-01-03 00:00:00 -0.551488  0.301711  -0.682017     1.4073
2013-01-04 00:00:00 -0.141452 -0.514909   -2.99863 -0.0283258
2013-01-05 00:00:00  -1.25555    1.1793   0.932212  -0.441398
2013-01-06 00:00:00  0.349649 -0.781957  0.0733772   0.810805

I would like to have the values corresponding to 0 and 1 as columns attributes. 我想将与0和1对应的值作为列属性。 For instance I would like to keep all columns that correspond to mammel (A,B,D) but i'm just interested in values corresponding to dates. 例如,我想保留所有与Mammel(A,B,D)对应的列,但我只对与日期对应的值感兴趣。

So, in other words, I'm trying to find a way to do subsets of the original datasets through combinations of keywords or values (which I call attributes here). 因此,换句话说,我正在尝试找到一种通过关键字或值(在此称为属性)的组合来处理原始数据集的子集的方法。

I believe the same is possible in R dataframe structures but I could'nt find the best pythonic / pandatic way to do so ! 我相信在R数据帧结构中也可以做到这一点,但是我找不到最佳的pythonic / pandatic方法!

Thanks in advance for any help provided ! 在此先感谢您提供的任何帮助!

Andrew's comment was exactly what I needed (and I owe you a big thanks for that) but I couldn't get the good words to find it myself. 安德鲁的评论正是我所需要的(为此我深表谢意),但是我自己却找不到好词。

The solution to my question was Multi-Indexing if someone ever reads this post ! 我的问题的解决方案是如果有人读过此帖子,则为Multi-Indexing!

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

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