import pandas as pd s = pd.Series(['a','b','c',None]) s 0 a 1 b 2 c 3 None dtype: object s = s.astype("string") s 0 a 1 b 2 c 3 <NA> dtype: string
类型转换:其他类型转string
import pandas as pd df2 = pd.DataFrame( [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, -4], [4, 5, 0], [7, -9, 9], ], columns=['a', 'b', 'c']) df2['a'] = df2['a'].astype("string") df2.dtypes a string b int64 c int64 dtype: object 所有列设置为字符串格式 df = df2.astype("string")
字符串处理
所有列设置为字符串格式 df = df.astype("string") True/False列表 s.str.contains("tx") s.str.startswith("tx") 条件成立的数据 s[s.str.startswith("tx")==True]
Pandas处理字符串方法汇总