回答編集履歴

2

Update

2022/03/07 10:41

投稿

melian
melian

スコア19865

test CHANGED
@@ -9,8 +9,8 @@
9
9
  dfx = (
10
10
  df1.groupby('col1', as_index=False)
11
11
  .apply(lambda x: pd.Series(
12
- x[x.columns[1:]].unstack()
12
+ x.iloc[:,1:].unstack()
13
- .values[~np.in1d(np.arange(x.shape[0]*(x.shape[1]-1)), [x.shape[0]*2])],
13
+ .values[~np.in1d(np.arange(x.shape[0]*(x.shape[1]-1)), [x.shape[0]*2])],
14
14
  index=[f'col{i}' for i in range(2,(x.shape[0]*x.shape[1]-1))])))
15
15
 
16
16
  print(dfx)

1

Update

2022/03/07 09:57

投稿

melian
melian

スコア19865

test CHANGED
@@ -6,12 +6,11 @@
6
6
  df1=pd.DataFrame({'col1': ['a', 'a','b','b'],'col2': range(4),'col3':['apple','5','bag','7'],'col4':['apple','sort','bag','table']})
7
7
  #df2=pd.DataFrame({'col1': ['a','b'],'col2': ['0','2'],'col3':['1','3'],'col4':['apple','bag'],'col5':['5','7'],'col6':['sort','table']})
8
8
 
9
- exclude = [4]
10
9
  dfx = (
11
10
  df1.groupby('col1', as_index=False)
12
11
  .apply(lambda x: pd.Series(
13
12
  x[x.columns[1:]].unstack()
14
- .values[~np.in1d(np.arange(x.shape[0]*(x.shape[1]-1)), exclude)],
13
+ .values[~np.in1d(np.arange(x.shape[0]*(x.shape[1]-1)), [x.shape[0]*2])],
15
14
  index=[f'col{i}' for i in range(2,(x.shape[0]*x.shape[1]-1))])))
16
15
 
17
16
  print(dfx)