Python 如何根据表中列的数据类型使用适当的值填充空值?
我正在看Python 如何根据表中列的数据类型使用适当的值填充空值?,python,pandas,Python,Pandas,我正在看pandas中的csv。现在我需要填充空值并将其转储到表中。这就是我要做的 import pandas as pd from sqlalchemy import create_engine df = pd.read_csv(file_path) df.fillna('', inplace=True) engine = create_engine('postgresql://username:******@localhost:****/database') df.to_sql("my_t
pandas
中的csv
。现在我需要填充空值并将其转储到表中。这就是我要做的
import pandas as pd
from sqlalchemy import create_engine
df = pd.read_csv(file_path)
df.fillna('', inplace=True)
engine = create_engine('postgresql://username:******@localhost:****/database')
df.to_sql("my_table", engine)
现在的问题是,对于具有integer
值且缺少少量值的列,pandas
为缺少的值填充空string
。因此,当将其转储到表中时,pandas
将列类型分类为字符串
,并将其转储到表中。因此,此列将文本
作为数据类型(在postgres
的情况下)获取,而不是不做任何事情来填充缺少的值,并且该列被正确分类为整数
或双精度
(在postgres
的情况下),这是正确的行为
但是,对于具有string
值以及缺少值的列,这不是问题,因为这些缺少的值将被分配一个空string
,并且不会影响列类型
现在我想要的是一种方法,对于那些具有整数或浮点值的列,用0
填充空值,对于那些具有string
值的列,用'
(空字符串)填充空值。我如何在熊猫身上做到这一点
注意:一些列也可以是
datetime
,我现在不打算用任何内容填充这些列。您可以这样做:
np.random.seed(47)
df = pd.DataFrame({'attend' : np.random.choice(['yes', 'no', 'some', np.nan], 100),
'other_random_col' : np.random.choice(['a', 'b', 'c', np.nan], 100),
'int_col' : np.random.sample(100),
'none' : [np.nan] * 100})
#checks all rows where columns are numbers and fills NaN with 0
df.loc[:, df.dtypes == np.number] = df.fillna(0)
也适用于字符串,但请注意:
- 要选择字符串,必须使用
数据类型,但请注意 这将返回所有对象数据类型列对象
数值
和字符串
列
注意:假设您只想用'
填充字符串数据类型列的N/A值,其余(数字列)用0
填充
df.transform(lambda x: x.fillna('') if x.dtype == 'object' else x.fillna(0))
案例2:需要自定义函数来处理更多数据类型 如果您想处理更多的数据类型,可以创建自己的函数并应用它来填充空值
def fill_null_values(value):
dtype = value.dtype
result = ''
# to handle string data type
if dtype == 'object':
result = ''
# to handle numeric data type
elif ('int' in dtype ) or ('float' in dtype ):
result = 0
# add more cases to handle more data type
return value.fillna(result)
data.transform(fill_null_values)