您可以使用list comprehension
:
blacklisted = ['Phase', 'clusterIndex']
cols = [col for col in df.columns if col not in blacklisted]
print (cols)
['28', '29', '30', '31', '32']
print (df[cols])
28 29 30 31 32
0 0.007871 0.004631 0.000963 0.000092 0.000438
1 0.003459 0.000730 0.000332 0.000012 0.000433
2 0.003261 0.002412 0.000852 0.000042 0.000202
3 0.001358 0.000313 0.000611 0.000029 0.000596
4 0.001713 0.000203 0.000069 0.000038 0.000069
5 0.001656 0.000041 0.000048 0.000221 0.000045
6 0.001348 0.000023 0.000107 0.000316 0.000109
7 0.001544 0.000194 0.000138 0.000829 0.000138
8 0.000359 0.000469 0.000278 0.000290 0.000279
9 0.000397 0.000351 0.000232 0.000449 0.000230
或者difference
:
blacklisted = ['Phase', 'clusterIndex']
cols = df.columns.difference(blacklisted)
print (cols)
Index(['28', '29', '30', '31', '32'], dtype='object')
print (df[cols])
28 29 30 31 32
0 0.007871 0.004631 0.000963 0.000092 0.000438
1 0.003459 0.000730 0.000332 0.000012 0.000433
2 0.003261 0.002412 0.000852 0.000042 0.000202
3 0.001358 0.000313 0.000611 0.000029 0.000596
4 0.001713 0.000203 0.000069 0.000038 0.000069
5 0.001656 0.000041 0.000048 0.000221 0.000045
6 0.001348 0.000023 0.000107 0.000316 0.000109
7 0.001544 0.000194 0.000138 0.000829 0.000138
8 0.000359 0.000469 0.000278 0.000290 0.000279
9 0.000397 0.000351 0.000232 0.000449 0.000230
Numpy solution
与numpy.setdiff1d
:
blacklisted = ['Phase', 'clusterIndex']
cols = np.setdiff1d(df.columns, blacklisted)
print (cols)
['28' '29' '30' '31' '32']
print (df[cols])
28 29 30 31 32
0 0.007871 0.004631 0.000963 0.000092 0.000438
1 0.003459 0.000730 0.000332 0.000012 0.000433
2 0.003261 0.002412 0.000852 0.000042 0.000202
3 0.001358 0.000313 0.000611 0.000029 0.000596
4 0.001713 0.000203 0.000069 0.000038 0.000069
5 0.001656 0.000041 0.000048 0.000221 0.000045
6 0.001348 0.000023 0.000107 0.000316 0.000109
7 0.001544 0.000194 0.000138 0.000829 0.000138
8 0.000359 0.000469 0.000278 0.000290 0.000279
9 0.000397 0.000351 0.000232 0.000449 0.000230
与drop
列解决方案:
blacklisted = ['Phase', 'clusterIndex']
print (df.drop(blacklisted, axis=1))
28 29 30 31 32
0 0.007871 0.004631 0.000963 0.000092 0.000438
1 0.003459 0.000730 0.000332 0.000012 0.000433
2 0.003261 0.002412 0.000852 0.000042 0.000202
3 0.001358 0.000313 0.000611 0.000029 0.000596
4 0.001713 0.000203 0.000069 0.000038 0.000069
5 0.001656 0.000041 0.000048 0.000221 0.000045
6 0.001348 0.000023 0.000107 0.000316 0.000109
7 0.001544 0.000194 0.000138 0.000829 0.000138
8 0.000359 0.000469 0.000278 0.000290 0.000279
9 0.000397 0.000351 0.000232 0.000449 0.000230
你应该在帖子中包括你所做的尝试,你当前的代码和更多的细节。如果不是,人们可能会认为你想要另一个人来解决你的整个问题 – Jalo