Pandas_对某列的内容分列
Employees表:
import pandas as pd employees = pd.read_excel("../018/Employees.xlsx",index_col="ID") employees .head()
对 Full Name 进行切割分列:
# df = employees["Full Name"].str.split() # str.split()不写参数,会把分隔的内容放在一个列表里 # df df = employees["Full Name"].str.split(expand=True) # expand=True 可以把用分割的内容直接分列 employees["FirstName"]=df[0] employees["LastName"]=df[1] print(employees)
部分结果图:
把分隔后的FistName 都变成大写
employees["FirstName"]=employees["FirstName"].str.upper() print(employees)
结果图:
把分隔的LastName都变成小写
employees["LastName"]=employees["LastName"].str.lower() print(employees)
结果图:
引申:更多Series.str 方法详见:http://pandas.pydata.org/pandas-docs/version/0.23.3/api.html#string-handling
Series.str.spilt: