Python:将null替换为0

时间:2020-11-12 15:27:24

标签: python for-loop null nan isnull

将Python数据框中的所有空“ NaN”值替换为0的最佳方法是什么?

还可以通过for循环来做到这一点吗?

1 个答案:

答案 0 :(得分:1)

您可以简单地使用

const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize({
    host: "127.0.0.1",
    port: 5432,
    database: "mydb",
    username: "myusername",
    password: "mypassword",
    dialect: "postgres",
    logging: console.log,
});

const schemaName = 'aschema';

async function main() {

    ////////////////////////////////////////////////////////////
    // Establish the connection
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');

    ////////////////////////////////////////////////////////////
    // Create db schema
    await sequelize.createSchema(schemaName);

    ////////////////////////////////////////////////////////////
    // Define models

    const Company = sequelize.define('company', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
    }, {
        schema: schemaName,
        // freezeTableName: true,
        tableName: 'company'
    });

    const User = sequelize.define('user', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING
        },
        companyId: {
            type: DataTypes.INTEGER
        },
        // aNewPotentialField: {
        //     type: DataTypes.INTEGER
        // }
    }, {
        schema: schemaName,
        // freezeTableName: true,
        tableName: 'user'
    });

    ////////////////////////////////////////////////////////////
    // Set relations
    User.belongsTo(Company, { as: 'company', foreignKey: 'companyId' });
    // Company.hasOne(User, { as: 'users', foreignKey: 'companyId' });

    // User.belongsTo(Company);
    ////////////////////////////////////////////////////////////
    // Sync

    // * These syncs below multiply the existing relations.
    // * Will add the field 'aNewPotentialField' in the future since alter set to true.
    await Company.sync({ alter: true });
    await User.sync({ alter: true });

    // * These syncs below don't multiply the existing relations.
    // * However, also wont add the field 'aNewPotentialField' in the future since alter set to false.
    // await Company.sync();
    // await User.sync();

    ////////////////////////////////////////////////////////////
    // Close the established connection.
    await sequelize.close();
    ////////////////////////////////////////////////////////////
}


main()
    .then(() => {
        console.log('App started successfully.');
    })
    .catch((e) => {
        throw e;
    });