python - 将浮点值更改为整数值,然后在 Pandas 数据框中连接

标签 python pandas dataframe

我有一个名为“sample”的数据框,它包含三列:“birthDay”、“birthMonth”和“birthYear”,并且包含浮点值,如下图所示:

enter image description here

我想添加新列“dateOfBirth”并以整数格式输入并获取以下数据框:

enter image description here

我尝试了 sample["dateOfBirth"] = sample["birthDay"].map(str)。 +"/"+ baseball["birthMonth"].map(str) +"/"+ baseball["birthYear"].map(str).但结果是 "11.0/3.0/1988.0""4.0/20.0/2001.0"

非常感谢您的帮助。

最佳答案

设置

sample = pd.DataFrame([
        [3., 11., 1988.],
        [20., 4., 2001.],
    ], columns=['birthDay', 'birthMonth', 'birthYear'])

选项 1
使 dateOfBirth 成为一系列 Timestamps

# dictionary map to rename to canonical date names
# enables convenient conversion using pd.to_datetime
m = dict(birthDay='Day', birthMonth='Month', birthYear='Year')
sample['dateOfBirth'] = pd.to_datetime(sample.rename(columns=m))

sample

enter image description here


选项 2
如果你坚持要一个字符串
dt 访问器与 strftime

一起使用
# dictionary map to rename to canonical date names
# enables convenient conversion using pd.to_datetime
m = dict(birthDay='Day', birthMonth='Month', birthYear='Year')

sample['dateOfBirth'] = pd.to_datetime(sample.rename(columns=m)) \
                          .dt.strftime('%-m/%-d/%Y')

sample

enter image description here


选项 3
如果你真的想从值中重建
使用 apply

f = '{birthMonth:0.0f}/{birthDay:0.0f}/{birthYear:0.0f}'.format
sample['dateOfBirth'] = sample.apply(lambda x: f(**x), 1)
sample

enter image description here


nulls
如果一个或多个日期列有缺失值:
选项 1 和 2 不需要任何更改,无论如何都是推荐的选项。
如果你想从 float 构造,我们可以使用 bool 掩码和 loc 来分配。

sample = pd.DataFrame([
        [3., 11., 1988.],
        [20., 4., 2001.],
        [20., np.nan, 2001.],
    ], columns=['birthDay', 'birthMonth', 'birthYear'])

sample

enter image description here

f = '{birthMonth:0.0f}/{birthDay:0.0f}/{birthYear:0.0f}'.format
mask = sample[['birthDay', 'birthMonth', 'birthYear']].notnull().all(1)
sample.loc[mask, 'dateOfBirth'] = sample.apply(lambda x: f(**x), 1)
sample

enter image description here


时间
给定样本
enter image description here

时间
给定采样时间 10,000
enter image description here

关于python - 将浮点值更改为整数值,然后在 Pandas 数据框中连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030317/

相关文章:

python - 如果我在 __init__.py 中有一些函数如何将此函数导入到当前目录代码中

python - django.db.utils.InterfaceError : (0, '' )

python - 当使用 W-MON 频率重新采样时,为什么 pandas 会向前滚动一周?

python - 如何计算 pandas 股票开盘价的百分比变化

python - 以编程方式将 Pandas 数据框切片

使用 UTF-8 header 的 Python 2.7 解码错误 : UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3

python - 如何将一行从一个 pandas 数据框复制到另一个 pandas 数据框?

python - 如何在遍历 pandas 数据框时创建新列并插入行值

python - 连接两个数据帧并根据条件删除重复行

python - 如何从另一个模型引用同一个模型两次?