如何根据python中DataFrame的列值选择列名? 非常感谢,
Set source = ActiveWorkbook.Sheets("Raw Data")
Set dest = ActiveWorkbook.Sheets("Filtered Data")
dest.Activate
dest.Range("A2:AD1048576").Delete
source.Activate
source.Range("A2:AD2").AutoFilter
Sleep 2000
If smu = "All" Then
source.Range("A2:AD2").AutoFilter field:=7
Else
source.Range("A2:AD2").AutoFilter field:=7, Criteria1:=smu, Operator:=xlFilterValues
End If
If rgn = "All" Then
source.Range("A2:AD2").AutoFilter field:=8
Else
source.Range("A2:AD2").AutoFilter field:=8, Criteria1:=rgn, Operator:=xlFilterValues
End If
Sleep 2000
If InSec = "All" Then
source.Range("A2:AD2").AutoFilter field:=9
Else
source.Range("A2:AD2").AutoFilter field:=9, Criteria1:=InSec, Operator:=xlFilterValues
End If
If market = "All" Then
source.Range("A2:AD2").AutoFilter field:=10
Else
source.Range("A2:AD2").AutoFilter field:=10, Criteria1:=market, Operator:=xlFilterValues
End If
Sleep 2000
If bu = "All" Then
source.Range("A2:AD2").AutoFilter field:=6
Else
source.Range("A2:AD2").AutoFilter field:=6, Criteria1:=subcomp, Operator:=xlFilterValues
End If
If look.Range("V1").Value = "PI-IT Advisory" Then
source.Range("A2:AD2").AutoFilter field:=11, Criteria1:=subcomp, Operator:=xlFilterValues
End If
Sleep 2000
lastraw = source.Range("A1048576").End(xlUp).Row
Sleep 3000
source.Range("A3:AD" & lastraw).SpecialCells(xlCellTypeVisible).Copy
dest.Activate
dest.Range("A2").PasteSpecial xlPasteValues
对于每一行,检查列值,如果值<= 2,则记录列名。
对于上面的示例,输出希望为:
import pandas as pd
foo = pd.DataFrame([['A',1,2,4],['B',3,4,2],['C',5,6,1]], columns=('a', 'x', 'y','z'))
foo.set_index('a')
Out[1]:
a x y z
A 1 2 4
B 3 4 2
C 5 6 1
答案 0 :(得分:0)
使用melt
,然后过滤值,然后使用assign
来为值cumcount
新建一个键
df=foo.melt('apple').\
loc[lambda x : x.value<=2].\
assign(value=lambda x : x.groupby('apple').cumcount()+1)
df.pivot('apple','value','variable')
Out[56]:
value 1 2
apple
A x y
B z NaN
C z NaN
答案 1 :(得分:0)
这是使用mul
用列标签替换值<= 2
的一种方法。然后排序,将空字符串放在每一行的末尾。
import pandas as pd
foo = pd.DataFrame([['A',1,2,4],['B',3,4,2],['C',5,6,1]], columns=('apple', 'x', 'y','z'))
arr = (foo.iloc[:, 1:] <= 2).mul(foo.columns[1:].to_series(), axis=1)
arr_sorted = arr.apply(sorted, key=bool, reverse=True, axis=1).values
foo.iloc[:, 1:] = pd.DataFrame(arr_sorted.tolist(), columns=foo.columns[1:])
print(foo)
apple x y z
0 A x y
1 B z
2 C z