程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python, append] change a column of data to percentage, and solve the problem that append replaces the previous data

編輯:Python

How to change a column of mixed data to percentage 、 And solve append Replace the previous data problem

df For a with multiple rows and columns dataframe, One of the columns df[' Yield '] There are many types of (float、NAN、 as well as string), I hope that df[' Yield '] Medium float Convert to percentage format , Other data remain the same .

The code is as follows :

def astype_percent(df): t = [] for i in df: use_dict = {} if type(i) == float: if not np.isnan(i): i = '%.2f%%' % (i * 100) if type(i) == str: use_dict = str(i) t.append(use_dict) return pd.Series(t)df[' Yield '] = astype_percent(df[' Yield '])

And found that df[' Yield '] The column as a whole moves forward one line , Cause dislocation 、 The last line is missing .
Checked how to solve it append Replace the previous data , But it didn't work


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved