我有以下代码行,用于删除DataFrame中的某些列。这些列中的某些列可能会在DataFrame中不存在。
const fs = require('fs')
module.exports = {
configureWebpack: config => {
if (process.env.NODE_ENV !== 'production') {
config.devServer = {
host: 'qzuku.test',
// https://medium.freecodecamp.org/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec
https: {
key: fs.readFileSync('./qzukuDevServer.key'),
cert: fs.readFileSync('./qzukuDevServer.crt'),
ca: fs.readFileSync(process.env.HOME + '/.ssh/rootDevCA.pem')
}
}
}
}
}
如何跳过不存在的列?
预期输出:
df =
col1 col2 col3
asa 132 4534
dfd 24 4252
cols = ["col1", "colB"]
df.drop(cols, inplace=True, axis=1)
答案 0 :(得分:4)
使用errors='ignore'
cols = ["col1", "colB"]
df.drop(cols, axis=1, errors='ignore', inplace=True)
df
Out[167]:
col2 col3
0 132 4534
1 24 4252
答案 1 :(得分:0)
import pandas as pd
col1 = ['asa', 'dfd']
col2 = [132, 24]
col3 = [4523, 4252]
cols = {'col1':col1,'col2':col2, 'col3':col3}
df = pd.DataFrame(cols)
drop_cols = ['col1', 'colB']
for col in drop_cols:
if col in df.columns.values:
df.drop(col, inplace = True, axis = 1)
df
返回您要查找的内容。
col2 col3
0 132 4523
1 24 4252