日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python 变量名重新赋值 变量重新赋值 通过字典的方式

發(fā)布時間:2025/4/5 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 变量名重新赋值 变量重新赋值 通过字典的方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對數(shù)據(jù)表的操作經(jīng)常需要重命名變量,或者中心賦值某個變量,可以使用numpy的where,也可以使用rename, 或者使用replace。

使用字典的方式比較方便。下面是代碼:

import pandas as pd import statsmodels.api as sm import numpy as np df = pd.read_excel("sn_short_for_reg.xlsx")df.keys() df['y_sat']df_new = df[df['y_sat'].isnull() == False]df_new.shapey_sat = df_new['y_sat'] y_saty2 = np.where(y_sat >0, 1, 0)y2 pd.Series(y2).value_counts()df_new['y2'] = y2df df_newdf_new_short = df_new[df_new['sat_default_idx'] == 0]df_new_short.shapedf_new_short df_new_shortx3 = df_new_short['x3_post_level'] x3.value_counts()d_x3 = {"市":2,"省":1,"區(qū)":3,"天津市":1,"北京市":1,"自治州":2,"縣":3,"盟":2,"重慶市":1,"上海市":1 }df_new_short['x3_post_level'].replace(d_x3) df_new_short['x3_post_level']df_new_short['x3_2'] =df_new_short['x3_post_level'].replace(d_x3)df_new_short.head()df_new_short.keys() df_new_short['x1_psot_length']d_newname = {"x1_psot_length":"x1_post_length" }df_new_short.rename(columns=d_newname)

數(shù)據(jù)表概覽

使用where賦值

?

使用字典 對變量重新賦值

結(jié)果如下:

使用字典重命名 變量,要使用inplace選項,不然數(shù)據(jù)表不會改

?

總結(jié)

以上是生活随笔為你收集整理的python 变量名重新赋值 变量重新赋值 通过字典的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。