在Python中,有一种方法可以逐渐修改我正在复制的行中的值吗?

huangapple go评论94阅读模式
英文:

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

使用您提到的问题中的设置:

  1. d = {
  2. '1': ['20', 'NYC', '2'],
  3. '2': ['30', 'NYC', '2'],
  4. '3': ['5', 'NYC', '2'],
  5. '4': ['300', 'LA', '2'],
  6. '5': ['30', 'LA', '2'],
  7. '6': ['100', 'LA', '2']
  8. }
  9. columns = ['Price', 'City', 'Quantity']
  10. # 创建数据框并重命名列
  11. df = pd.DataFrame.from_dict(
  12. data=d, orient='index'
  13. )
  14. df.columns = columns

使用来自该问题的最受欢迎的答案来复制行:

  1. df = df.loc[df.index.repeat(df.Quantity)]

您可以通过按索引分组并使用 cumcount 来增加价格,如下所示:

  1. df['Price Increase'] = df.groupby(df.index).cumcount() + 1
  2. df['New Price'] = df['Price Increase'] + df['Price'].astype(float)

给出结果:

  1. Price City Quantity Price Increase New Price
  2. 1 20 NYC 2 1 21.0
  3. 1 20 NYC 2 2 22.0
  4. 2 30 NYC 2 1 31.0
  5. 2 30 NYC 2 2 32.0
  6. 3 5 NYC 2 1 6.0
  7. 3 5 NYC 2 2 7.0
  8. 4 300 LA 2 1 301.0
  9. 4 300 LA 2 2 302.0
  10. 5 30 LA 2 1 31.0
  11. 5 30 LA 2 2 32.0
  12. 6 100 LA 2 1 101.0
  13. 6 100 LA 2 2 102.0
  • 我为了清晰起见保留了一些额外的列,但如果您希望如此,您可以将价格替换为 New Price 并删除额外的列。
英文:

Using the Setup from the question you referred to:

  1. d = {
  2. '1': ['20', 'NYC', '2'],
  3. '2': ['30', 'NYC', '2'],
  4. '3': ['5', 'NYC', '2'],
  5. '4': ['300', 'LA', '2'],
  6. '5': ['30', 'LA', '2'],
  7. '6': ['100', 'LA', '2']
  8. }
  9. columns=['Price', 'City', 'Quantity']
  10. # create dataframe and rename columns
  11. df = pd.DataFrame.from_dict(
  12. data=d, orient='index'
  13. )
  14. df.columns = columns

Using the most upvoted answer from that question to duplicate the rows:

  1. df = df.loc[df.index.repeat(df.Quantity)]

You can increase the price by grouping by the index and using cumcount as follows:

  1. df['Price Increase'] = df.groupby(df.index).cumcount() + 1
  2. df['New Price'] = df['Price Increase'] + df['Price'].astype(float)

Giving the result:

  1. Price City Quantity Price Increase New Price
  2. 1 20 NYC 2 1 21.0
  3. 1 20 NYC 2 2 22.0
  4. 2 30 NYC 2 1 31.0
  5. 2 30 NYC 2 2 32.0
  6. 3 5 NYC 2 1 6.0
  7. 3 5 NYC 2 2 7.0
  8. 4 300 LA 2 1 301.0
  9. 4 300 LA 2 2 302.0
  10. 5 30 LA 2 1 31.0
  11. 5 30 LA 2 2 32.0
  12. 6 100 LA 2 1 101.0
  13. 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

  1. 假设您有像我的虚拟DataFrame上的Product列那样的唯一键我会这样做
  2. ```python
  3. import pandas as pd
  4. import numpy as np
  5. # 创建虚拟DataFrame
  6. df = pd.DataFrame(np.random.randint(low=0, high=10, size=(4, 2)), columns=["Quantity", "Price"])
  7. df["Product"] = np.arange(4)
  8. df = df[["Product", "Quantity", "Price"]]
  9. print("原始DataFrame".center(30, '-'))
  10. print(df)
  11. print()
  12. # 创建重复的列
  13. df = df.loc[df.index.repeat(df['Quantity'])]
  14. # 在新的DataFrame上更改价格
  15. df["Price"] = df.groupby("Product", group_keys=False)["Price"].apply(lambda x: x + np.arange(x.shape[0]))
  16. df.index = np.arange(df.shape[0])
  17. print("更改后的DataFrame".center(30, '-'))
  18. print(df)
  1. <details>
  2. <summary>英文:</summary>
  3. Assuming you have a unique key like the &quot;Product&quot; column on my dummy DataFrame I&#39;d do something like this:
  4. ```python
  5. import pandas as pd
  6. import numpy as np
  7. # Dummy DataFrame creation
  8. df = pd.DataFrame(np.random.randint(low=0, high=10, size=(4, 2)), columns=[&quot;Quantity&quot;, &quot;Price&quot;])
  9. df[&quot;Product&quot;] = np.arange(4)
  10. df = df[[&quot;Product&quot;, &quot;Quantity&quot;, &quot;Price&quot;]]
  11. print(&quot;Original DataFrame&quot;.center(30, &#39;-&#39;))
  12. print(df)
  13. print()
  14. # Creating repeated columns
  15. df = df.loc[df.index.repeat(df[&#39;Quantity&#39;])]
  16. # Changing price on new df
  17. df[&quot;Price&quot;] = df.groupby(&quot;Product&quot;, group_keys=False)[&quot;Price&quot;].apply(lambda x: x + np.arange(x.shape[0]))
  18. df.index = np.arange(df.shape[0])
  19. print(&quot;Changed DataFrame&quot;.center(30, &#39;-&#39;))
  20. print(df)

huangapple
  • 本文由 发表于 2023年7月17日 21:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定