英文:
In Python, is there a way to progressively modify a value in a row that I am duplicating?
问题
使用https://stackoverflow.com/questions/32792263/duplicate-row-based-on-value-in-different-column作为框架。
由于我正在基于“数量”列的值复制行,是否有一种方法可以修改复制行的值?例如,对于第一行,当行被复制时,我想将“价格”的值从20更改为21,如果您要复制该行3次,我想要“价格”值为23,依此类推。
英文:
Using https://stackoverflow.com/questions/32792263/duplicate-row-based-on-value-in-different-column as a framework.
As I am duplicating the rows based on the value of the Quantity column, is there a way to then modify the value of the duplicated row? As an example, for the first row, when the row is duplicated I want to change the value of the Price from 20 to 21 and if you were to duplicate the row 3 times, I would want the Price value to be 23, so on and so forth.
答案1
得分: 1
使用您提到的问题中的设置:
d = {
'1': ['20', 'NYC', '2'],
'2': ['30', 'NYC', '2'],
'3': ['5', 'NYC', '2'],
'4': ['300', 'LA', '2'],
'5': ['30', 'LA', '2'],
'6': ['100', 'LA', '2']
}
columns = ['Price', 'City', 'Quantity']
# 创建数据框并重命名列
df = pd.DataFrame.from_dict(
data=d, orient='index'
)
df.columns = columns
使用来自该问题的最受欢迎的答案来复制行:
df = df.loc[df.index.repeat(df.Quantity)]
您可以通过按索引分组并使用 cumcount 来增加价格,如下所示:
df['Price Increase'] = df.groupby(df.index).cumcount() + 1
df['New Price'] = df['Price Increase'] + df['Price'].astype(float)
给出结果:
Price City Quantity Price Increase New Price
1 20 NYC 2 1 21.0
1 20 NYC 2 2 22.0
2 30 NYC 2 1 31.0
2 30 NYC 2 2 32.0
3 5 NYC 2 1 6.0
3 5 NYC 2 2 7.0
4 300 LA 2 1 301.0
4 300 LA 2 2 302.0
5 30 LA 2 1 31.0
5 30 LA 2 2 32.0
6 100 LA 2 1 101.0
6 100 LA 2 2 102.0
- 我为了清晰起见保留了一些额外的列,但如果您希望如此,您可以将价格替换为
New Price
并删除额外的列。
英文:
Using the Setup from the question you referred to:
d = {
'1': ['20', 'NYC', '2'],
'2': ['30', 'NYC', '2'],
'3': ['5', 'NYC', '2'],
'4': ['300', 'LA', '2'],
'5': ['30', 'LA', '2'],
'6': ['100', 'LA', '2']
}
columns=['Price', 'City', 'Quantity']
# create dataframe and rename columns
df = pd.DataFrame.from_dict(
data=d, orient='index'
)
df.columns = columns
Using the most upvoted answer from that question to duplicate the rows:
df = df.loc[df.index.repeat(df.Quantity)]
You can increase the price by grouping by the index and using cumcount as follows:
df['Price Increase'] = df.groupby(df.index).cumcount() + 1
df['New Price'] = df['Price Increase'] + df['Price'].astype(float)
Giving the result:
Price City Quantity Price Increase New Price
1 20 NYC 2 1 21.0
1 20 NYC 2 2 22.0
2 30 NYC 2 1 31.0
2 30 NYC 2 2 32.0
3 5 NYC 2 1 6.0
3 5 NYC 2 2 7.0
4 300 LA 2 1 301.0
4 300 LA 2 2 302.0
5 30 LA 2 1 31.0
5 30 LA 2 2 32.0
6 100 LA 2 1 101.0
6 100 LA 2 2 102.0
*I left some extra columns for clarity, but you could just replace price with the New Price
and remove the additional columns, if that is what you want.
答案2
得分: 0
假设您有像我的虚拟DataFrame上的“Product”列那样的唯一键,我会这样做:
```python
import pandas as pd
import numpy as np
# 创建虚拟DataFrame
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(4, 2)), columns=["Quantity", "Price"])
df["Product"] = np.arange(4)
df = df[["Product", "Quantity", "Price"]]
print("原始DataFrame".center(30, '-'))
print(df)
print()
# 创建重复的列
df = df.loc[df.index.repeat(df['Quantity'])]
# 在新的DataFrame上更改价格
df["Price"] = df.groupby("Product", group_keys=False)["Price"].apply(lambda x: x + np.arange(x.shape[0]))
df.index = np.arange(df.shape[0])
print("更改后的DataFrame".center(30, '-'))
print(df)
<details>
<summary>英文:</summary>
Assuming you have a unique key like the "Product" column on my dummy DataFrame I'd do something like this:
```python
import pandas as pd
import numpy as np
# Dummy DataFrame creation
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(4, 2)), columns=["Quantity", "Price"])
df["Product"] = np.arange(4)
df = df[["Product", "Quantity", "Price"]]
print("Original DataFrame".center(30, '-'))
print(df)
print()
# Creating repeated columns
df = df.loc[df.index.repeat(df['Quantity'])]
# Changing price on new df
df["Price"] = df.groupby("Product", group_keys=False)["Price"].apply(lambda x: x + np.arange(x.shape[0]))
df.index = np.arange(df.shape[0])
print("Changed DataFrame".center(30, '-'))
print(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论