简体   繁体   English

使列行 python

[英]make columns rows python

hello i have the following code:你好我有以下代码:

for j in range(8):                                                         
    b=fran[fran.Año.isin([2020]) & fran.Channel.isin(['CANAL 5'])&fran.Week.isin([j])]  
    c=b[['hour','number']]  
    print(c)  

I get the output:我得到 output:

    |hour| number
   1|12-1|3.1
   2|1-3 |2.3
   3|3-7 |4.6
    |hour| number
   4|7-11|2
   1|12-1|1.2
   2|1-3 |3
   3|3-7 |1.1
   4|7-11|5.6
   ...
    |hour| number
   1|12-1|1
   2|1-3 |1.2
   3|3-7 |5.4
   4|7-11|2.2

I would like help to get the following output:我想帮助获得以下 output:

 | hour | number1| number2|...|numbern|
1|12-1  |3.1     | 1.2    |...| 1
2|1-3   |2.3     | 3      |...| 1.2
3|3-7   |4.6     | 1.1    |...| 5.4
4|7-11  |2       | 5.6    |...| 2.2

Change your code to将您的代码更改为

l=[]
for j in range(8):                                                         
    b=fran[fran.Año.isin([2020]) & fran.Channel.isin(['CANAL 5'])&fran.Week.isin([j])]  
    l.append(b[['hour','number']].set_index('hour').rename(columns={'number' : 'number' + str(j)}))

Then do concat然后做concat

df=pd.concat(l),axis=1).reset_index()

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

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