检查dataframe中的某列是否为整数,如果是整数,则必须乘以10
import numpy as np
import pandas as pd
df = pd.dataframe(....)
#function to check and multiply if a column is integer
def xtimes(x):
for col in x:
if type(x[col]) == np.int64:
return x[col]*10
else:
return x[col]
#using apply to apply that function on df
df.apply(xtimes).head(10)
我收到这样的错误 ('GP', 'occurred at index school')
最佳答案
您可以使用 select_dtypes
得到数字列,然后相乘。
In [1284]: df[df.select_dtypes(include=['int', 'int64', np.number]).columns] *= 10
您可以拥有
include=[... np.int64, ..., etc]
的特定检查 list 。
关于pandas - 检查数据框中的列是否为整数,并执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45110412/