简体   繁体   English

如何根据总值创建 n 行数

[英]How to create n-number of rows based off a total value

I am looking to generate multiple rows based off a single record from a list.我希望根据列表中的单个记录生成多行。

For example, I have a CSV file (eg File A ) as follows:例如,我有一个 CSV 文件(例如文件 A )如下:

User ID用户身份 Total Value总价值 Multiple Value多值 Remaining Value剩余价值
123 123 1007.25 1007.25 11 11 7.25 7.25
456 456 804.25 804.25 9 9 4.25 4.25

I want to create another CSV file (eg File B ) like this:我想创建另一个 CSV 文件(例如文件 B ),如下所示:

User ID用户身份 Final Value最终价值
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 100.00 100.00
123 123 7.25 7.25
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 100.00 100.00
456 456 4.25 4.25

I tried using the pandas.concat function, along with a for loop, but I can't seem to get that to work properly (it errors out).我尝试使用 pandas.concat function 以及 for 循环,但我似乎无法使其正常工作(它出错了)。

If I understand the logic correctly, you can do this without the columns "Multiple Value" and "Remaining Value":如果我理解正确的逻辑,你可以在没有“多个值”和“剩余值”列的情况下执行此操作:

import numpy as np
import pandas as pd

df = pd.read_clipboard() # Your df here

df["Final Value"] = df["Total Value"].apply(lambda x: np.minimum(x - np.arange(0, x, 100), 100))

out = df[["User ID", "Final Value"]].explode("Final Value")
   User ID Final Value
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123       100.0
0      123        7.25
1      456       100.0
1      456       100.0
1      456       100.0
1      456       100.0
1      456       100.0
1      456       100.0
1      456       100.0
1      456       100.0
1      456        4.25

This could be a solution:这可能是一个解决方案:

User_ID_List = df["User ID"].to_list()

Multiple_Value_List = df["Multiple Value"].to_list()

Remaining_Value_List = df["Remaining Value"].to_list()

New_User_ID_List= []

New_Multiple_Value_List = []


for x in range(len(User_ID_List):
  Multiple_Value = Multiple_Value_List[x]
  for y in range(Multiple_Value):
     New_User_ID_List.append(User_ID_List[x])
     New_Multiple_Value_List.append(Multiple_Value_List[x])
  New_Multiple_Value_List.pop() 
  New_Multiple_Value_List.append(Remaining_Value_List[x])

df = pd.DataFrame()
df["User Id"] = New_User_ID_List
df["Final Value"] = New_Multiple_Value_List

Perhaps something like this?也许是这样的?

import numpy as np

def spread(g):
    tot, rem, n = g[['Total Value', 'Remaining Value', 'Multiple Value']].squeeze()
    n -= 1
    val = (tot - rem) / n
    return np.r_[np.repeat(val, n), rem]

out = df.groupby('User ID').apply(spread).explode().to_frame('Final Value')

>>> out
        Final Value
User ID            
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123      100.0     
123       7.25     
456      100.0     
456      100.0     
456      100.0     
456      100.0     
456      100.0     
456      100.0     
456      100.0     
456      100.0     
456       4.25     

Then:然后:

>>> print(out.to_csv())
User ID,Final Value
123,100.0
123,100.0
...

Or rather: out.to_csv(my_file_b) .或者更确切地说: out.to_csv(my_file_b)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM