1,pd.to_datetime( 要轉換的日期, format= ),
2,pd.to_datetime.today( ).year ,pd.to_datetime.now( ).year
3,字符串的astype方法:字段類型轉換,
4,日期.dt.year :必須加dt
5,刪除字段: df. drop ([ ] , axis=1, inplace=True)
df是數據框的名字,列表[ ] 里是要刪除的字段名,
axis=1表示在水平方向,因為默認drop方法是用來刪除數據庫中的行記錄,
inplace=True表示會影響原數組
6,序列的apply ( ) 方法 , apply( func= , )
7,序列使用.str后,可以轉換為字符串類型
df=pd.read_excel(r'E:\Python學習資料\data_test03.xlsx') # print(df) print("df的類型:",type(df)) print("數據集DataFrame的維數:",df.ndim) print("元素類型:\n",df.dtypes) print("\n元素個數:",df.size) #轉換字段的數據類型 df.birthday=pd.to_datetime(df.birthday,format='%Y-%m-%d') df.tel=df.tel.astype("str") #將手機號中間4位隱藏起來 df.tel=df.tel.apply(func=lambda x:x.replace(x[3:7],"*****")) #添加域名domain,年齡age,工齡workage,專業profession df['domain']=df.email.apply(func=lambda x:x.split("@")[1]) df['age']=pd.datetime.today().year-df.birthday.dt.year df['workage']=pd.datetime.today().year-df.birthday.dt.year df['profession']=df.other.str.findall("專業:(.*?),") #這種方法能去掉專業外面的 中括號 df['major']=df.other.str.findall("專業:(.*?),").str[0] #刪除'birthday','start_work','other'這幾個變量 df.drop(['birthday','start_work','other'],axis=1,inplace=True) #各種操作之后的數據集 print("\n",df)

