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
|
简易实现
df_res.loc[:,"result_sim"] = df_res["result_sim"].apply(lambda x: float(f"{x:.4g}"))*100
|
|
to_numeric
import pandas as pd
# 示例 DataFrame
df = pd.DataFrame({'col_str': ['1', '2', '3', '4.5']})
# 转换为数字
df['col_num'] = pd.to_numeric(df['col_str'])
print(df.dtypes) # 查看类型
处理无法转换的值
# 方法1:将无法转换的值设为 NaN
df['col_num'] = pd.to_numeric(df['col_str'], errors='coerce')
# 方法2:忽略无法转换的值(保持原样)
df['col_num'] = pd.to_numeric(df['col_str'], errors='ignore')
# 方法3:遇到无法转换的值报错
df['col_num'] = pd.to_numeric(df['col_str'], errors='raise') # 默认行为
astype() # 转换为浮点数 df['col_float'] = df['col_str'].astype(float) # 转换为整数 df['col_int'] = df['col_str'].astype(int) # 注意:不能有小数部分 特殊字符
# 移除美元符号
df['col_clean'] = df['col_str'].str.replace('$', '')
df['col_num'] = pd.to_numeric(df['col_clean'])
# 移除百分号并转换为小数
df['col_clean'] = df['col_str'].str.replace('%', '')
df['col_num'] = pd.to_numeric(df['col_clean']) / 100
转换整个 DataFrame # 转换整个 DataFrame 中的所有可转换的列 df = df.apply(pd.to_numeric, errors='ignore') |
|
|
|
|
|
|
类型转换:其他类型转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处理字符串方法汇总