python - pandas shift 将我的列从整数转换为 float 。

标签 python pandas numpy

shift 将我的列从整数转换为 float 。事实证明 np.nan 只是 float 。有什么方法可以将移位后的列保持为整数吗?

df = pd.DataFrame({"a":range(5)})
df['b'] = df['a'].shift(1)

df['a']
# 0    0
# 1    1
# 2    2
# 3    3
# 4    4
# Name: a, dtype: int64

df['b']

# 0   NaN
# 1     0
# 2     1
# 3     2
# 4     3
# Name: b, dtype: float64

最佳答案

pandas 0.24以下解决方案:

问题是你得到 NaN 值什么是 float,所以 int 被转换为 float - 见 na type promotions .

一种可能的解决方案是将 NaN 值转换为某些值,如 0,然后可能转换为 int:

df = pd.DataFrame({"a":range(5)})
df['b'] = df['a'].shift(1).fillna(0).astype(int)
print (df)
   a  b
0  0  0
1  1  0
2  2  1
3  3  2
4  4  3

Pandas 0.24+ 的解决方案 - 检查 Series.shift:

fill_value object, optional
The scalar value to use for newly introduced missing values. the default depends on the dtype of self. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used.

Changed in version 0.24.0.

df['b'] = df['a'].shift(fill_value=0)

关于python - pandas shift 将我的列从整数转换为 float 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870093/

相关文章:

python - 如何计算 Numpy 数组中某个范围内的值?

python - 带有 predict_proba 的 SGDClassifier

python 帮助!如果/否则语句

python - 将 pandas 数据框中列中的 # 值替换为按行自动递增的值

python - 有效地重新排列 2D NumPy 数组

python - 如何从另一个数组中减去一个数组的每个元素?

python - 测试没有用户名的 AbstractUser 的 Django create_user

python - 在 mac os 上为 python3 安装 mysqlclient for mariadb

python - 在 Pandas 中格式化 DataFrame - 堆叠列

python - MultiLabelBinarizer 可以表示值的计数吗?