簡體   English   中英

用列拆分熊貓數據框

[英]splitting pandas dataframe with columns

我有一個帶有大量列的數據框。 我真正想要的是創建/拆分數據框。 例如:

產生玩具數據:

df = pd.DataFrame(np.arange(10),columns = ['x'])
df['y'] = np.arange(30,40,1)
df['1'] = np.random.rand(10)
df['2'] = np.random.rand(10)
df['3'] = np.random.rand(10)
df['4'] = np.random.rand(10)
df['5'] = np.random.rand(10)

df =

    x   y   1              2           3          4            5
0   0   30  0.047787    0.435396    0.926836    0.314469    0.477411
1   1   31  0.083536    0.258120    0.682284    0.025050    0.713777
2   2   32  0.201041    0.872864    0.050977    0.580314    0.185589
3   3   33  0.105833    0.974538    0.559265    0.128242    0.217965
4   4   34  0.146551    0.662001    0.936995    0.050702    0.249724
5   5   35  0.098615    0.854952    0.649501    0.509777    0.726458
6   6   36  0.387889    0.040331    0.902277    0.051822    0.354042
7   7   37  0.321591    0.823724    0.052266    0.081491    0.187576
8   8   38  0.983665    0.152271    0.530755    0.384810    0.844386
9   9   39  0.649185    0.776682    0.239589    0.654547    0.581337

我真正想要的是按如下所示的方式拆分數據幀:

df1 =

    x   y   1
0   0   30  0.047787
1   1   31  0.083536
2   2   32  0.201041
3   3   33  0.105833
4   4   34  0.146551
5   5   35  0.098615
6   6   36  0.387889
7   7   37  0.321591
8   8   38  0.983665
9   9   39  0.649185

df2 =

    x    y    2
0   0   30  0.435396
1   1   31  0.25812
2   2   32  0.872864
3   3   33  0.974538
4   4   34  0.662001
5   5   35  0.854952
6   6   36  0.040331
7   7   37  0.823724
8   8   38  0.152271
9   9   39  0.776682

等等。 由於我有大量的列,因此很難一一完成。 有沒有更簡單的方法可以做到這一點?

先感謝您。

您可以設置xy col作為索引軸始終保持靜態,然后跨列執行groupby

通過利用字典理解,遍歷每個這樣的組。 另外,最后的reset_index將確保生成平坦的DF

df.set_index(['x','y'], inplace=True)
dfs = {i:grp.reset_index() for i, grp in df.groupby(np.arange(len(df.columns)), axis=1)}

產生的結果字典的鍵將構成可以查詢的列名:

dfs[0]

在此處輸入圖片說明

dfs[1]

在此處輸入圖片說明

等等。

您可以使用列表推導自動生成數據幀:

df_cuts = [df[['x', 'y', col]] for col in df.columns if col not in ['x', 'y']]

我在命令行中驗證了輸出:

for i in range(len(df_cuts)):
    print 'df %r:' % i
    print df_cuts[i]
    print '\n'

結果是這樣的:

df 0:
   x   y         1
0  0  30  0.695465
1  1  31  0.425572
2  2  32  0.018986
3  3  33  0.165947
4  4  34  0.103120
5  5  35  0.069060
6  6  36  0.676640
7  7  37  0.492231
8  8  38  0.950436
9  9  39  0.156195


df 1:
   x   y         2
0  0  30  0.928538
1  1  31  0.019624
2  2  32  0.862811
3  3  33  0.289581
4  4  34  0.150975
5  5  35  0.835313
6  6  36  0.768760
7  7  37  0.396042
8  8  38  0.423745
9  9  39  0.268596


df 2:
   x   y         3
0  0  30  0.999175
1  1  31  0.004125
2  2  32  0.137457
3  3  33  0.042903
4  4  34  0.580698
5  5  35  0.663723
6  6  36  0.996608
7  7  37  0.960361
8  8  38  0.932486
9  9  39  0.758873


df 3:
   x   y         4
0  0  30  0.708976
1  1  31  0.547635
2  2  32  0.722322
3  3  33  0.912707
4  4  34  0.380471
5  5  35  0.607742
6  6  36  0.803980
7  7  37  0.569364
8  8  38  0.882297
9  9  39  0.954743


df 4:
   x   y         5
0  0  30  0.900532
1  1  31  0.247818
2  2  32  0.629371
3  3  33  0.502218
4  4  34  0.183292
5  5  35  0.875611
6  6  36  0.940828
7  7  37  0.200641
8  8  38  0.874052
9  9  39  0.525997

對我來說,您可以將index設置為['x','y'] ,然后按列名獲取列:

>>> df2 = df.set_index(['x','y'])
>>> df2
             1         2         3         4
x y                                         
0 30  0.161017  0.280965  0.058429  0.750003
1 31  0.643460  0.258441  0.951750  0.774355
2 32  0.948439  0.573363  0.126369  0.577629
3 33  0.896542  0.722825  0.927644  0.470369
4 34  0.298559  0.009676  0.841103  0.899220
5 35  0.855292  0.849880  0.529132  0.929805
6 36  0.428680  0.486381  0.271048  0.219826
7 37  0.752370  0.698653  0.980554  0.894405
8 38  0.027857  0.085865  0.086936  0.403528
9 39  0.522483  0.646266  0.825819  0.574025

>>> df2['1']
x  y 
0  30    0.161017
1  31    0.643460
2  32    0.948439
3  33    0.896542
4  34    0.298559
5  35    0.855292
6  36    0.428680
7  37    0.752370
8  38    0.027857
9  39    0.522483

如果只需要遍歷各列,則可以執行以下操作:

>>> for i in range(1,5):
...     print df[['x','y',str(i)]]
... 
   x   y         1
0  0  30  0.161017
1  1  31  0.643460
2  2  32  0.948439
3  3  33  0.896542
4  4  34  0.298559
5  5  35  0.855292
6  6  36  0.428680
7  7  37  0.752370
8  8  38  0.027857
9  9  39  0.522483
   x   y         2
0  0  30  0.280965
1  1  31  0.258441
2  2  32  0.573363
3  3  33  0.722825
4  4  34  0.009676
5  5  35  0.849880
6  6  36  0.486381
7  7  37  0.698653
8  8  38  0.085865
9  9  39  0.646266
   x   y         3
0  0  30  0.058429
1  1  31  0.951750
2  2  32  0.126369
3  3  33  0.927644
4  4  34  0.841103
5  5  35  0.529132
6  6  36  0.271048
7  7  37  0.980554
8  8  38  0.086936
9  9  39  0.825819
   x   y         4
0  0  30  0.750003
1  1  31  0.774355
2  2  32  0.577629
3  3  33  0.470369
4  4  34  0.899220
5  5  35  0.929805
6  6  36  0.219826
7  7  37  0.894405
8  8  38  0.403528
9  9  39  0.574025

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM