迭代列表错误

时间:2018-01-17 20:43:24

标签: python pandas

I have a dataframe,delf
     Date        inp  name   
0  2017-08-07  2.3.6  ABC 
1  2017-08-07  2.3.6  ABC      
2  2017-08-08  2.3.6  TAC         
3  2017-08-22  2.5.9  TTT         
4  2017-09-23  0.8.0  TAC         
5  2017-10-09  2.3.6  ABC         
6  2017-10-09  2.3.6  TAC
7  2017-10-09  2.3.6  TAC                  
8  2017-10-23  0.8.0  TAC         
9  2017-11-08  6.2.6  ABC        

then another dataframe,trex
               2.3.6ABC  2.3.6TAC      2.5.9TTT      
August            2         1            0            
September         0         0            0             
October           1         2            0             
November          0         0            1             

another dataframe,dher
              2.3.6ABC  2.3.6TAC       
August            2         1                      
September         0         0                         
October           1         2                        
November          0         0                        

我想在列中获取不同的值:DELF的'inp',在本例中为4.然后,在这种情况下,TREX和DHER的列数为3和2。如何存储2个数据帧(TREX和DHER)的列数,然后在df1中获取TREX和DHER百分比。它应该是这样的:

                 noOfColumn      pct
  TREX                 3         3/4=75                      
  DHER                 2         2/4=50

我尝试使用此代码:

df_list = [TREX,DHER]
idx, v = [], []
for i, df in enumerate(df_list, 1):
idx.append('{}'.format())
v.append(len(df.columns))

然后,

df = pd.DataFrame(v, index=idx, columns=['noOfColumn'])
df['pct'] = df['noOfColumn'] / DELF.inp.nunique()

没有给我正确的输出

1 个答案:

答案 0 :(得分:1)

您可以为df指定名称

nil?