【发布时间】:2022-01-23 12:52:56
【问题描述】:
我有一个 ID 号数据框(n = 140,但可能或多或少),我有 5 位组长。需要为每个组长随机分配一定数量的这些 id(为方便起见,让 n=28,但我需要能够控制数量)并且这些行需要拆分为新的 df然后从原始数据帧中删除,以便领导者之间没有交叉。
import pandas as pd
import numpy as np
#making the df
df = pd.DataFrame()
df['ids'] = np.random.randint(1, 140, size=140)
df['group_leader'] = ''
# list of leader names
leaders = ['John', 'Paul', 'George', 'Ringo', 'Apu']
我可以为每个领导者这样做
df.loc[df.sample(n=28).index, 'group_leader'] = 'George'
g = df[df['group_leader']=='George'].copy()
df = df[df['group_leader] != 'George']
print(df.shape()[0]) #double checking that df has less ids in it
但是,为每个组长单独执行此操作似乎真的不符合 Python 标准(并不是说我是这方面的专家),并且不容易重构为函数。
我认为我可以使用 dict 和 for loop 来做到这一点
frames = dict.fromkeys('group_leaders', pd.DataFrame())
for i in frames.keys(): #allows me to fill the cells with the string key?
df.loc[df.sample(n=28).index, 'group_leader'] = str(i)
frames[i].update(df[df['group_leader']== str(i)].copy())#also tried append()
print(frames[i].head())
df = df[df['group_leader'] != str(i)]
print(f'df now has {df.shape[0]} ids left') #just in case there's a remainder of ids
但是,新的数据框仍然是空的,我得到了错误:
Traceback (most recent call last):
File "C:\Users\path\to\the\file\file.py", line 38, in <module>
df.loc[df.sample(n=28).index, 'group_leader'] = str(i)
File "C:\Users\path\to\the\file\pandas\core\generic.py", line 5356, in sample
locs = rs.choice(axis_length, size=n, replace=replace, p=weights)
File "mtrand.pyx", line 909, in numpy.random.mtrand.RandomState.choice
ValueError: a must be greater than 0 unless no samples are taken
这让我相信我做错了两件事:
- 要么错误地制作字典,要么错误地更新它。
- 使 for 循环运行时尝试运行 1 的次数过多。
我试图尽可能清楚并提供我需要的最低限度的有用版本,任何帮助将不胜感激。
注意 - 我知道 5 可以很好地划分为 140,在某些情况下可能并非如此,但我很确定如果需要,我可以通过 if-else 自己处理。
【问题讨论】:
标签: python pandas dataframe dictionary refactoring