简体   繁体   English

Pandas 只从另一列复制值直到某个日期

[英]Pandas copy values from another column only upto a certain date

I have a column Date of type datetime64[ns] consisting of one row per date .我有一个datetime64[ns]类型的Date列,每个 date 由一行组成。 And two more columns A and B .还有两列AB Suppose the dates start from 1st Dec 2019 and goes on till 29th Feb 2020, I wish to copy all the values from column A to column B upto a certain date x.假设日期从 2019 年 12 月 1 日开始一直持续到 2020 年 2 月 29 日,我希望将所有值从A列复制到B列,直到某个日期 x。

For example, if x = 15th Feb 2020, I need to copy values of A from 1st Dec 2019 to 14th Feb 2020 into column B.例如,如果 x = 2020 年 2 月 15 日,我需要将 A 的值从 2019 年 12 月 1 日到 2020 年 2 月 14 日复制到 B 列中。

Any help appreciated!任何帮助表示赞赏!

I think you need Series.mask for copy data less like x , so mask is created by Series.lt :我认为你需要Series.mask来复制数据而不像x ,所以 mask 是由Series.lt创建的:

np.random.seed(2020)

rng = pd.date_range('2019-12-01', '2020-02-29')
df = pd.DataFrame({'Date': rng, 
                   'A': np.random.randint(10, size=91),
                   'B': np.random.randint(10, size=91)})  
print (df.head(20))
         Date  A  B
0  2019-12-01  0  5
1  2019-12-02  8  2
2  2019-12-03  3  4
3  2019-12-04  6  3
4  2019-12-05  3  0
5  2019-12-06  3  9
6  2019-12-07  7  8
7  2019-12-08  8  2
8  2019-12-09  0  0
9  2019-12-10  0  6
10 2019-12-11  8  7
11 2019-12-12  9  1
12 2019-12-13  3  7
13 2019-12-14  7  2
14 2019-12-15  2  5
15 2019-12-16  3  9
16 2019-12-17  6  6
17 2019-12-18  5  7
18 2019-12-19  0  9
19 2019-12-20  4  3

x = '2019-12-10'

df['B'] = df['B'].mask(df["Date"].lt(x), df['A'])
print (df.head(20))
         Date  A  B
0  2019-12-01  0  0
1  2019-12-02  8  8
2  2019-12-03  3  3
3  2019-12-04  6  6
4  2019-12-05  3  3
5  2019-12-06  3  3
6  2019-12-07  7  7
7  2019-12-08  8  8
8  2019-12-09  0  0
9  2019-12-10  0  6
10 2019-12-11  8  7
11 2019-12-12  9  1
12 2019-12-13  3  7
13 2019-12-14  7  2
14 2019-12-15  2  5
15 2019-12-16  3  9
16 2019-12-17  6  6
17 2019-12-18  5  7
18 2019-12-19  0  9
19 2019-12-20  4  3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果值等于某个数字,则将值从一列复制到另一列 - Copy values from one column to another if values equal to a certain number Pandas 有条件地将值从一列复制到另一行 - Pandas conditionally copy values from one column to another row 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas 大熊猫使用另一列中的值移动日期 - pandas shift date using values from another column 将某些子字符串从一列复制到另一列? - Copy Certain substrings from one column to another? Pandas 系列仅将 NaN 填充到一定限度 - Pandas Series fill NaNs upto a certain limit only 使用 pandas 将之前的行加起来最多为 3 并与另一列的值相乘 - Sum up previous rows upto 3 and multiply with value from another column using pandas 将某些 pandas dataframe 列值从一列移到另一列,并将旧的 position 替换为 Nan - Move certain pandas dataframe column values from one column to another and replace old position with Nan 如何根据 pandas 中两个不同列的条件将值从一列复制到另一列? - how to copy values from one column into another column based on conditions of two different columns in pandas? 创建一个新的 pandas dataframe 列,其中包含一个附加值,然后是其下方另一列的值的副本 - Creating a new pandas dataframe column with one added value, then a copy of values from another column underneath it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM