我想從數(shù)據(jù)框中刪除幾列,其中幾列是連續(xù)的,可以使用 df.loc 進行分組。但是我也有一些列名需要使用列名本身來刪除。有人可以幫助我,在同一個 drop sattement 中同時使用 df.loc 和列名。df_modified = df.drop(df.loc[:, 'Quarter of Joining': 'Age in Company (Years)'].columns, axis =1)我還想用他們的名字刪除更多列,即說'DOB','Gender'
1 回答

繁星點點滴滴
TA貢獻1803條經(jīng)驗 獲得超3個贊
在我看來,您的解決方案是最簡單的,這里是替代方案:
another = ['DOB','Gender']
m1 = (df.columns.values == 'Quarter of Joining').cumsum() != 0
m2 = (df.columns.values[::-1] == 'Age in Company (Years)').cumsum() != 0
df_modified = df = df.drop(df.columns[m1 & m2[::-1]].union(another), axis=1)
您的解決方案應該修改:
another = ['DOB','Gender']
c = df.loc[:, 'Quarter of Joining': 'Age in Company (Years)'].columns
df_modified = df.drop(c.union(another), axis=1)
添加回答
舉報
0/150
提交
取消