简体   繁体   English

向数据框中添加新行,其中一列保持不变,而另一列更改值

[英]Add new rows to data frame, where one column stays the same while other column changes values

sorry, if my title sounds a bit confusing.抱歉,如果我的标题听起来有点混乱。 What I'm basically trying to do is adding new rows in a data frame, where I duplicate the value of each unique value of one column, while another column's new values are changing.我基本上想做的是在数据框中添加新行,我复制一列的每个唯一值的值,而另一列的新值正在改变。

This is what my data frame looks like:这是我的数据框的样子:

id ID year
01 01 2022 2022
02 02 2022 2022
03 03 2022 2022
... ... ... ...
99 99 2022 2022

And I want it to look like this:我希望它看起来像这样:

id ID year
01 01 2022 2022
01 01 2023 2023
01 01 2024 2024
02 02 2022 2022
02 02 2023 2023
02 02 2024 2024
03 03 2022 2022
... ... ... ...
99 99 2024 2024

Ie I want for every id to add the years 2023 and 2024 in the year column.即我希望每个 id 在年列中添加 2023 年和 2024 年。 I tried doing this with an apply function, but it always didn't work out, could you guys help me out in solving this?我试过用apply函数做这个,但总是不行,你们能帮我解决这个问题吗?

years = [2022 + i for i in range(3)]
# or
years = [2022,2023, 2024]
pd.DataFrame({
    'id': np.repeat((data:=df.id.to_numpy()), len(years)).reshape(-1,len(years)).flatten(),
    'year': np.repeat(np.array(years), data.shape[0]).reshape(len(years), data.shape[0]).T.flatten()
})

一个快速的解决方案是制作当前数据框的两个副本,并将年份日期相应地更改为 2023 年和 2024 年。之后,使用pd.concat将所有 3 个数据集连接在一起。

You can simply make a list comprehension and concat all dataframe years wirh increments of your desire.您可以简单地进行列表理解并根据您的需求增加所有数据帧年份。 For example:例如:

pd.concat([df.assign(year=df.year+increment) for increment in range(0,3)]).sort_values(by='id').reset_index(drop=True)

This will increment your dataframe to three years as follows.这会将您的数据框增加到三年,如下所示。 You can play around with range for the desired number of extensions:您可以使用范围来获得所需的扩展数量:

id ID year
1 1 2022 2022
1 1 2023 2023
1 1 2024 2024
2 2 2022 2022
2 2 2023 2023
2 2 2024 2024
3 3 2022 2022
3 3 2023 2023
3 3 2024 2024

暂无
暂无

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

相关问题 向数据框中添加一个新列,其中值取决于同一列的前一行值 - Add a new column to a data-frame where values are dependent on previous row values of the same column 熊猫计算一列中的值,而另一列保持不变 - Pandas count values in one column where another column stays the same 如何根据其他行值添加 pandas 数据框列 - How to add pandas data frame column based on other rows values 将一个数据框的所有重复列值添加到 Pandas 中的另一个数据框 - Add all column values repeated of one data frame to other in pandas 如果满足基于同一数据帧中其他2列的行值的条件,则在数据帧的列行中填充值 - Filling values in rows of column in a data frame, if condition based on 2 other columns row values in the same data frame is met 如何使用 pandas 数据框将数据框的每一列值添加到一张一张的新工作表中 - How to add each column of a data frame values in one by one new sheets using pandas data frame 添加两个数据框,但仅添加几个选定的列,并且仅当其他列的值相同时 - Add two data frame but only a few selected column and only when other column values are the same Pandas - 创建新列,其中的值取自同一数据框中的其他行 - Pandas - Create new column where values are taken from other rows in the same dataframe 从 Pandas 数据框中选择多行,其中一列包含一些作为 NaN 的值 - Select multiple rows from pandas data frame where one of column contains some values as NaN Pandas,如何将一行中的值与同一列中的所有其他行进行比较,并将其作为新列中的新行值添加? - Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM