【发布时间】:2019-12-07 10:10:04
【问题描述】:
我使用此代码将浮点数转换为整数,但是它不起作用。以下是我到目前为止所经历的所有步骤:
Step 1: I converted timestamp1 and timestamp2 to datetime in order subtract and get days:
a=pd.to_datetime(df['timestamp1'], format='%Y-%m-%dT%H:%M:%SZ')
b=pd.to_datetime(df['timestamp2'], format='%Y-%m-%dT%H:%M:%SZ')
df['delta'] = (b-a).dt.days
Step 2: Converted the strings into integers as the day:
df['delta'] = pd.to_datetime(df['delta'], format='%Y-%m-%d', errors='coerce')
df['delta'] = df['delta'].dt.day
Step 3: I am trying to convert floats into integers.
categorical_feature_mask = df.dtypes==object
categorical_cols = df.columns[categorical_feature_mask].tolist()
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df[categorical_cols] = df[categorical_cols].apply(lambda col: le.fit_transform(col))
df[categorical_cols].head(10)
However, it throws an error TypeError: ('argument must be a string or number', 'occurred at index col1')
【问题讨论】:
-
您的列是字符类型而不是浮点数,将变量与自身进行比较是什么意思 x == x 将始终评估为 True。
-
@adnanmuttaleb 谢谢。你能帮忙解决一下吗?
标签: python-3.x pandas floating-point integer