如何在pandas列中自动用数字替换字符串?

时间:2018-12-19 15:29:43

标签: python pandas encode

我有一个这样的DF:

ID     City
0      1
1      2
2      1
3      3
4      4
5      2
.      .
.      .

我希望每个独特的城市都成为一个独特的数字。

所以结果可能是这样的:

class Cookiebot {

    public function __construct() {
        $this->get_cookiebot_id();
        $this->cookiebot();
        $this->save_cookiebot_settings();
        add_action( 'wp_head', array( $this, 'enqueue_script' ) );
        add_shortcode( 'wp_care_cookiebot_policy', array( $this, 'cookiebot_policy_shortcode' ) );
    }

public function cookiebot() {
    ?>
    <form method="POST">
        <div class="notice">
            <h3><?php _e( 'Cookiebot', 'wp-care' ); ?></h3>
            <table class="table table--full-width">
                <tr>
                    <th><?php _e( 'Your Cookiebot ID', 'wp-care' ); ?></th>
                    <td>
                    <?php echo $this->get_cookiebot_id(); ?>
                        <input type="text" id="wp_care_cookiebot_id" name="wp_care_cookiebot_id" value="<?php echo $this->get_cookiebot_id(); ?>" class="regular-text">
                        <p class="description"><?php _e( 'Change your settings on', 'wp-care' ); ?> <a href="https://manage.cookiebot.com/en/manage" target="_blank">Cookiebot</a></p>
                    </td>
                </tr>
                <tr>
                    <th><?php _e( 'Cookiebot Policy shortcode', 'wp-care' ); ?></th>
                    <td><code>[wp_care_cookiebot_policy]</code></td>
                </tr>
                <tr>
                    <th><?php _e( 'Disable Cookiebot on the website. (not recommended)', 'wp-care' ); ?></th>
                    <td><input type="checkbox" name="" value=""></td>
                </tr>
            </table>
        </div>
        <p><input type="submit" value="Save Settings" class="button button-primary"></p>
    </form>
    <?php
}

public function get_cookiebot_id() {
    $cookiebot_id = get_option( 'wp_care_cookiebot_id' );
    return $cookiebot_id;
}

public function save_cookiebot_settings() {
    if ( isset( $_POST['wp_care_cookiebot_id'] ) ) {
        $cookiebot_id = $_POST['wp_care_cookiebot_id'];
        var_dump( $cookiebot_id );
        update_option( 'wp_care_cookiebot_id', $cookiebot_id );
    }
}

public function set_wpml_language() {
    if ( function_exists( 'icl_object_id' ) ) {
        global $sitepress;
        echo 'data-culture="' . $sitepress->get_current_language() . '"';
    }
}

public function enqueue_script() {
    $cookiebot_id = get_option( 'wp_care_cookiebot_id' );
    if ( $cookiebot_id ) {
        ?>
    <script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" <?php $this->set_wpml_language(); ?> data-cbid="<?php echo $cookiebot_id; ?>" type="text/javascript" async></script>
    <?php } ?>
    <?php
}

public function cookiebot_policy_shortcode( $output ) {
    $cookiebot_id = get_option( 'wp_care_cookiebot_id' );
    if ( $cookiebot_id ) {
        ob_start();
        ?>
        <script id="CookieDeclaration" <?php $this->set_wpml_language(); ?> src="https://consent.cookiebot.com/<?php echo $cookiebot_id; ?>/cd.js" type="text/javascript" async></script>
        <?php
    }
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
    ?>
    <?php
}
}

我有一个非常大的城市数据集,所以字典+ apply方法将花费很长时间。有没有更快的方法?

0 个答案:

没有答案