简体   繁体   English

如何调整数据框以调整某些条件?(除以列,将结构更改为T)

[英]How can I split the dataframe adjusting some condition?(divid by columns change structure by T)

How can I split the dataframe adjusting some condition?(divid by columns change structure by T) 如何调整数据框以调整某些条件?(除以列,将结构更改为T)

I have dataframe like 我有像

   df:

        id   f1   c1     c2

   0    a    x     1     2
   1    b    x     3     4
   3    c    x     5     6
   4    a    y     7     8
   5    b    y     9     10
   6    c    y     11    12

and expected result 和预期的结果

   dfX (filtered f1=x value):

       newid   a     b     c

   0    c1     1     3     5
   1    c2     2     4     6

  dfY (filtered f1=y value):

   0    c1     7     9     11
   1    c2     8     10    12

I need to split dataframe by f1 value and change the sturutre 我需要通过f1值拆分数据帧并更改sturutre

I tried with 5~6 for loop and more than 20~30 lines and did it. 我尝试使用5〜6进行循环和超过20〜30行,并做到了。

But it seems it's not the best way to do it. 但这似乎并不是最好的方法。 It would be appreciated if you give me some tips to handle the data more efficent way 如果您给我一些技巧以更有效地处理数据,将不胜感激

#Set ID as index and transpose the DF and rename the column name.
df[df.f1=='x'][['id','c1','c2']].set_index('id').T.reset_index().rename(columns={'index':'newid'})
Out[115]: 
id newid  a  b  c
0     c1  1  3  5
1     c2  2  4  6

Similarly you can filter f1 on 'y': 同样,您可以在'y'上过滤f1:

df[df.f1=='y'][['id','c1','c2']].set_index('id').T.reset_index().rename(columns={'index':'newid'})
Out[120]: 
id newid  a   b   c
0     c1  7   9  11
1     c2  8  10  12

You can use groupby. 您可以使用groupby。

>>> for _, b in df.groupby(['f1']):
...     print b.pivot_table(columns='id').reset_index().rename(columns={'index':'newid'})
...     
id newid  a  b  c
0     c1  1  3  5
1     c2  2  4  6
id newid  a   b   c
0     c1  7   9  11
1     c2  8  10  12

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

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