创建/操作 pandas 数据框架,使用多个列表?

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

Creating/Manipulating a pandas dataframe from multiple lists?

问题

我需要创建一个包含四列的数据框以进行进一步处理。每列的范围为0-100,间隔为1(这些是百分比)。这是容易的部分。不太容易的部分(对我来说不太容易,对其他人可能相对简单)是,每一行的校验和总是必须为100。这显然会将行数从100扩展到100*100*100*100

例如:

100 0 0 0
99  1 0 0
99  0 0 1
87  4 5 4
...

我尝试首先创建四个列表

lstA = list(range(0, 101, 1))
lstB = list(range(0, 101, 1))

然后将它们转换为数据框,但我不知道如何在数据框上运行校验和操作,同时对列进行排序并相应地附加生成的额外行。

英文:

I need to create a dataframe with four columns for further processing. Each column has a range from 0-100 with increments of 1 (those are percentages). That's the easy part. The not-so-easy part (for me. for others probably rather trivial) is, that the checksum of each row always has to be 100. That obviously expands the number of rows from 100 to 100*100*100*100.

For example:

100 0 0 0
99  1 0 0
99  0 0 1
87  4 5 4
...

I've tried creating four lists first

lstA = list(range(0, 101, 1))
lstB = list(range(0, 101, 1))

and then a df out of them, but i'm at a loss how i'm supposed to run that checksum operation on the dataframe, while also sorting the columns and appending the resulting additional rows accordingly.

答案1

得分: 0

需要循环遍历前3个值的不同可能性,并计算第四个值。

out = []
for i in range(101):
for j in range(101-i):
for k in range(101-i-j):
out.append([i, j, k, 100-i-j-k])

df = pd.DataFrame(out)

*注意:有效组合的数量为**176851**。*

输出:
      0  1  2    3

0 0 0 0 100
1 0 0 1 99
2 0 0 2 98
3 0 0 3 97
4 0 0 4 96
... ... .. .. ...
176846 98 2 0 0
176847 99 0 0 1
176848 99 0 1 0
176849 99 1 0 0
176850 100 0 0 0

[176851 rows x 4 columns]


<details>
<summary>英文:</summary>

You need to loop over the different possibilities for the first 3 values and compute the fourth one

out = []
for i in range(101):
for j in range(101-i):
for k in range(101-i-j):
out.append([i, j, k, 100-i-j-k])

df = pd.DataFrame(out)

*NB. note that number of valid combinations is **176851**.*

Output:
      0  1  2    3

0 0 0 0 100
1 0 0 1 99
2 0 0 2 98
3 0 0 3 97
4 0 0 4 96
... ... .. .. ...
176846 98 2 0 0
176847 99 0 0 1
176848 99 0 1 0
176849 99 1 0 0
176850 100 0 0 0

[176851 rows x 4 columns]


</details>



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

发表评论

匿名网友

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

确定