6

次のデータフレームがあります。

id ABC   
1 34353 917998 ×        
2 34973 980340 ×      
3 87365 498097 ×      
4 98309 486547 ×      
5 87699 475132         
6 52734 4298894         
7 8749267 4918066 ×    
8 89872 18103         
9 589892 4818086 年    
10 765 4063 年
11 32369 418165 年
12 206 2918137    
13 554 3918072    
14 1029 1918051 ×
15 2349243 4918064

5,6 などの空の行のセットごとに、新しいデータ フレームを作成します。複数のデータ フレームを生成する必要があります。以下に示すように:

ID AB
5 87699 475132         
6 52734 4298894
ID AB
8 89872 18103      
ID AB
12 206 2918137    
13 554 3918072          
ID AB
15 2349243 4918064          
4

3 に答える 3

6
isnull = df.C.isnull()
partitions = (isnull != isnull.shift()).cumsum()

gb = df[isnull].groupby(partitions)

NaNこの時点で、 inの連続するグループごとに個別のデータフレームを作成するという目標を達成しましたdf。これらはgb.get_group()、各キーのメソッドを介してアクセスできますgb.groups

確認するために、表示を連結します。

keys = gb.groups.keys()
dfs = pd.concat([gb.get_group(g) for g in keys], keys=keys)
dfs

ここに画像の説明を入力

のセットアップdf

@Alberto Garcia-Raboso のリーダーを使用しました

import io
import pandas as pd

# Create your sample dataframe
data = io.StringIO("""\
id       A        B        C   
1      34353    917998     x        
2      34973    980340     x      
3      87365    498097     x      
4      98309    486547     x      
5      87699    475132         
6      52734    4298894         
7      8749267  4918066    x    
8      89872    18103         
9      589892   4818086    y    
10     765      4063       y 
11     32369    418165     y
12     206      2918137    
13     554      3918072    
14     1029     1918051    x
15     2349243  4918064
""")
df = pd.read_csv(data, delim_whitespace=True)
于 2016-07-12T05:51:52.787 に答える
1

これは、少し複雑で、おそらくあまり高速ではない を使用したソリューションですitertools.groupby(これは、連続した同様の値のシーケンスをまとめることで有名です)。

from itertools import groupby
import io

import pandas as pd

# Create your sample dataframe
data = io.StringIO("""\
id       A        B        C   
1      34353    917998     x        
2      34973    980340     x      
3      87365    498097     x      
4      98309    486547     x      
5      87699    475132         
6      52734    4298894         
7      8749267  4918066    x    
8      89872    18103         
9      589892   4818086    y    
10     765      4063       y 
11     32369    418165     y
12     206      2918137    
13     554      3918072    
14     1029     1918051    x
15     2349243  4918064
""")
df = pd.read_csv(data, delim_whitespace=True)

# Create a boolean column that encodes which rows you want to keep
df['grouper'] = df['C'].notnull()

# Isolate the indices of the rows you want to keep, grouped by contiguity
groups = [list(map(lambda x: x[1]['id'], list(l)))
              for k, l in groupby(df.iterrows(), key=lambda x: x[1]['grouper'])
              if not k]
print(groups)     # => [[5, 6], [8], [12, 13], [15]]

# Gather the sub-dataframes whose indices match `groups`
dfs = []
for g in groups:
    dfs.append(df[['A', 'B']][df['id'].isin(g)])

# Inspect what you got
for df in dfs:
    print(df)

出力:

       A        B
4  87699   475132
5  52734  4298894
       A      B
7  89872  18103
      A        B
11  206  2918137
12  554  3918072
          A        B
14  2349243  4918064
于 2016-07-11T22:56:43.893 に答える