1 float与str的互化

import pandas as pd
import numpy as np df = pd.DataFrame({'a':[1.22, 4.33],
'b':[3.44, 5.66]})
# 将float类型转为str
# 法一
df['a'] = df['a'].apply(lambda x: str(x))
print(type(df['a'].values[1]))
# 法二
df['b'] = df['b'].astype(str)
print(type(df['b'].values[1]))
# str转float同上
df['a'] = df['a'].apply(lambda x: float(x))
print(type(df['a'].values[1]))
df['b'] = df['b'].astype(float)
print(type(df['b'].values[1]))
# <class 'str'>
# <class 'str'>
# <class 'numpy.float64'>
# <class 'numpy.float64'>

2 建表时,如果赋的是空值,默认是float类型,所以要指定数据类型。

特别是建了空表后,与其它表融合时,要注意数据类型的改变。

df = pd.DataFrame({ 'a': [],
'b': [],
})
print(df.info())
df = pd.DataFrame({ 'a': [],
'b': [],
}, dtype = np.int64)
print(df.info())
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 0 entries
# Data columns (total 2 columns):
# a 0 non-null float64
# b 0 non-null float64
# dtypes: float64(2)
# memory usage: 76.0 bytes
# None
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 0 entries
# Data columns (total 2 columns):
# a 0 non-null int64
# b 0 non-null int64
# dtypes: int64(2)
# memory usage: 76.0 bytes
# None

参考:https://blog.csdn.net/jinruoyanxu/article/details/79150896

05-26 14:47