pandas列分割-添加单位-删除单位
列分割
data = {'地区':['中国','巴西'],'变电站':['变电站-A','变电站-B']} df = pd.DataFrame(data,index=['A','B']) # 原生方法 data2 = [x.split('-') for x in df['变电站']] df2 = pd.DataFrame(data2,index=df.index,columns=['变电站','标志']) # df2 = pd.DataFrame((x.split('-') for x in df['变电站']),index=df.index,columns=['变电站','标志']) # 自带方法 df3 = df['变电站'].str.split('-',expand=True)
# df3 = df['变电站'].str.split('-',expand=True)[1] 分割后只想要第二个
df3.columns = ['变电站','标志']
添加单位
import pandas as pd import numpy as np data = {'地区':['河南','安徽'],'产量':['2000','1200']} df = pd.DataFrame(data,index=['A','B']) df['产量'] = df['产量'] + '吨'
删除单位
import pandas as pd import numpy as np data = {'地区':['河南','安徽'],'产量':['2000吨','1200吨']} df = pd.DataFrame(data,index=['A','B']) df['产量'] = df['产量'].str[:-1]
还可以lambda方法
df["Percent Growth"] = df["Percent Growth"].apply(lambda x: x.replace("%","")).astype("float")/100
参考文章:https://www.jianshu.com/p/4a2ecf65e3ea