【问题标题】:How to organize data of several datasets into the same dataframe using pandas in Python?如何在 Python 中使用 pandas 将多个数据集的数据组织到同一个数据框中?
【发布时间】:2017-01-07 22:06:36
【问题描述】:

我在使用 Python 中的 pandas 将一些数据按我想要的方式组织在数据框中时遇到了麻烦。

我想要一个数据框,其中数据将被组织成三列(例如TimeVI)。

但是,我希望将不同样本的数据放在同一个数据框中,以便我可以轻松地从Sample#1Sample#2 中选择数据。

我想到的是这样的:

df1 = pd.DataFrame({'Time': np.arange(0,10,0.5), 'V': np.random.rand(20), 'I': np.random.rand(20)})
df1['Sample']= 'sample_1'

df2 = pd.DataFrame({'Time': np.arange(0,10,0.5), 'V': np.random.rand(20), 'I': np.random.rand(20)})
df2['Sample']= 'sample_2'

df = df1.append(df2)

请注意,我添加了另一个名为 Sample 的列来跟踪哪些数据对应于哪个样本。

但是我不知道如何从 df 调用来自sample_1sample_2 的数据

我该怎么做?这是组织数据的正确方法吗?我应该使用MultiIndex吗?

【问题讨论】:

  • 我会使用您的选项,然后选择带有df.loc[df["Sample"] == "sample_1",:] 的示例。它更加标准并且拥有干净的数据很重要 (vita.had.co.nz/papers/tidy-data.pdf)。

标签: python pandas indexing dataframe multi-index


【解决方案1】:

是的,MultiIndex 是一种可能的解决方案:

np.random.seed(1)
df1 = pd.DataFrame({'Time': np.arange(0,10,0.5), 
                    'V': np.random.rand(20), 
                    'I': np.random.rand(20)})

np.random.seed(2)
df2 = pd.DataFrame({'Time': np.arange(0,10,0.5), 
                    'V': np.random.rand(20), 
                    'I': np.random.rand(20)})

#print (df1)
#print (df2)

您可以将concat 全部DataFrames 转换为一个并在参数keys 中指定每个源DataFrame

print (pd.concat([df1, df2], keys=('sample_1','sample_2')))
                    I  Time         V
sample_1 0   0.800745   0.0  0.417022
         1   0.968262   0.5  0.720324
         2   0.313424   1.0  0.000114
         3   0.692323   1.5  0.302333
         4   0.876389   2.0  0.146756
         5   0.894607   2.5  0.092339
         6   0.085044   3.0  0.186260
         7   0.039055   3.5  0.345561
         8   0.169830   4.0  0.396767
         9   0.878143   4.5  0.538817
         10  0.098347   5.0  0.419195
         11  0.421108   5.5  0.685220
         12  0.957890   6.0  0.204452
         13  0.533165   6.5  0.878117
         14  0.691877   7.0  0.027388
         15  0.315516   7.5  0.670468
         16  0.686501   8.0  0.417305
         17  0.834626   8.5  0.558690
         18  0.018288   9.0  0.140387
         19  0.750144   9.5  0.198101
sample_2 0   0.505246   0.0  0.435995
         1   0.065287   0.5  0.025926
         2   0.428122   1.0  0.549662
         3   0.096531   1.5  0.435322
         4   0.127160   2.0  0.420368
         5   0.596745   2.5  0.330335
         6   0.226012   3.0  0.204649
         7   0.106946   3.5  0.619271
         8   0.220306   4.0  0.299655
         9   0.349826   4.5  0.266827
         10  0.467787   5.0  0.621134
         11  0.201743   5.5  0.529142
         12  0.640407   6.0  0.134580
         13  0.483070   6.5  0.513578
         14  0.505237   7.0  0.184440
         15  0.386893   7.5  0.785335
         16  0.793637   8.0  0.853975
         17  0.580004   8.5  0.494237
         18  0.162299   9.0  0.846561
         19  0.700752   9.5  0.079645

可以通过xs 选择数据 - 请参阅cross section

print (df.xs('sample_1', level=0))
           I  Time         V
0   0.800745   0.0  0.417022
1   0.968262   0.5  0.720324
2   0.313424   1.0  0.000114
3   0.692323   1.5  0.302333
4   0.876389   2.0  0.146756
5   0.894607   2.5  0.092339
6   0.085044   3.0  0.186260
7   0.039055   3.5  0.345561
8   0.169830   4.0  0.396767
9   0.878143   4.5  0.538817
10  0.098347   5.0  0.419195
11  0.421108   5.5  0.685220
12  0.957890   6.0  0.204452
13  0.533165   6.5  0.878117
14  0.691877   7.0  0.027388
15  0.315516   7.5  0.670468
16  0.686501   8.0  0.417305
17  0.834626   8.5  0.558690
18  0.018288   9.0  0.140387
19  0.750144   9.5  0.198101

如果需要只选择一些列:

print (df.xs('sample_1', level=0)[['Time','I']])
    Time         I
0    0.0  0.800745
1    0.5  0.968262
2    1.0  0.313424
3    1.5  0.692323
4    2.0  0.876389
5    2.5  0.894607
6    3.0  0.085044
7    3.5  0.039055
8    4.0  0.169830
9    4.5  0.878143
10   5.0  0.098347
11   5.5  0.421108
12   6.0  0.957890
13   6.5  0.533165
14   7.0  0.691877
15   7.5  0.315516
16   8.0  0.686501
17   8.5  0.834626
18   9.0  0.018288
19   9.5  0.750144

另一种解决方案是使用IndexSlice - 请参阅using slicers

idx = pd.IndexSlice
print (df.loc[idx['sample_1',:], ['Time','I']])
             Time         I
sample_1 0    0.0  0.800745
         1    0.5  0.968262
         2    1.0  0.313424
         3    1.5  0.692323
         4    2.0  0.876389
         5    2.5  0.894607
         6    3.0  0.085044
         7    3.5  0.039055
         8    4.0  0.169830
         9    4.5  0.878143
         10   5.0  0.098347
         11   5.5  0.421108
         12   6.0  0.957890
         13   6.5  0.533165
         14   7.0  0.691877
         15   7.5  0.315516
         16   8.0  0.686501
         17   8.5  0.834626
         18   9.0  0.018288
         19   9.5  0.750144

如果需要删除Multiindex的第一级:

idx = pd.IndexSlice
print (df.loc[idx['sample_1',:], ['Time','I']].reset_index(level=0, drop=True))
    Time         I
0    0.0  0.800745
1    0.5  0.968262
2    1.0  0.313424
3    1.5  0.692323
4    2.0  0.876389
5    2.5  0.894607
6    3.0  0.085044
7    3.5  0.039055
8    4.0  0.169830
9    4.5  0.878143
10   5.0  0.098347
11   5.5  0.421108
12   6.0  0.957890
13   6.5  0.533165
14   7.0  0.691877
15   7.5  0.315516
16   8.0  0.686501
17   8.5  0.834626
18   9.0  0.018288
19   9.5  0.750144

【讨论】:

  • 谢谢。我想这正是我正在寻找的。但是,考虑到我已将数据帧连接到一个新的“df”中,我如何仅从 sample_1 打印“时间”和“我”列?
  • 感谢您的接受。我想问你。给我一秒钟,我添加它来回答。
  • 谢谢。那很棒。我认为df.loc['sample_1'][['Time', 'I']] 也可以。
猜你喜欢
  • 2022-06-19
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 2012-09-14
  • 2021-10-06
相关资源
最近更新 更多