gpt4 book ai didi

python - 使用条件条件与 pandas 一起执行 numpy where

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:23 25 4
gpt4 key购买 nike

我在字段 t1_months 中有一个值用于计算支出发生的月数。我对该值充满信心,并希望将其用作要填充的分配数量的限制。

使用此代码我收到错误 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

所以这让我认为我需要重新调整这一行,因为这是代码停止的行。
if count <= counter:

我怎样才能创建这样的条件,但仍然能够使用 np.where 子句?

for month_num in range(1, 13):
count = 0
# counter = df['t1_months']
if count <= df['t1_months']:
if count <= counter:
df['t1_' + str(month_num) + '/1/2016'] = np.where(
(df["StartMonthYear"] <= pd.to_datetime(str(month_num) + '/1/2016'))
& (df["EndMonthYear"] >= pd.to_datetime(str(month_num) + '/1/2016')), df["t1_allotment"], '0.0')
count += 1

如果我有 df = pd.DataFrame( {'t1_months' : [12,10,6,7]})
的数据框如何使用 12 作为第一行中的检查来仅填充 12 个分配,第二行中 10 个,第三行中 6 个,最后一行中 7 个?

预期输出如下:

t1_months 1/1/2016 ... 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 12/1/2016
12 500 ... 500 500 500 500 500 500 500
10 500 ... 500 500 500 500 500 500
500 500 0 0
6 500 ... 500 500 500 500 0 0 0 0 0 0
7 500 ... 500 500 500 500 500 0 0 0 0 0

最佳答案

来源 DF:

In [77]: d
Out[77]:
t1_months
0 12
1 10
2 6
3 7

选项 1:

In [78]: d.join(d.t1_months.apply(lambda x: pd.Series([500]*x)).fillna(0)) \
...: .rename(columns=lambda x: '{}/1/2016'.format(x+1) if isinstance(x,int) else x)
Out[78]:
t1_months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \
0 12 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0
1 10 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 0.0
2 6 500.0 500.0 500.0 500.0 500.0 500.0 0.0 0.0 0.0 0.0 0.0
3 7 500.0 500.0 500.0 500.0 500.0 500.0 500.0 0.0 0.0 0.0 0.0

12/1/2016
0 500.0
1 0.0
2 0.0
3 0.0

选项 2:

In [87]: d.join(pd.DataFrame([[500]*x for x in d.t1_months],
...: columns=['{}/1/2016'.format(i) for i in range(1,13)],
...: index=d.index))
...:
Out[87]:
t1_months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \
0 12 500 500 500 500 500 500 500.0 500.0 500.0 500.0 500.0
1 10 500 500 500 500 500 500 500.0 500.0 500.0 500.0 NaN
2 6 500 500 500 500 500 500 NaN NaN NaN NaN NaN
3 7 500 500 500 500 500 500 500.0 NaN NaN NaN NaN

12/1/2016
0 500.0
1 NaN
2 NaN
3 NaN

更新:

Say that I have this DF though.

df = pd.DataFrame( {'months': [12,10,6,7], 'allot': [200, 500, 347, 192]}) .

How do I replace the value of 500 with that which is in the df['allot'] row so that the first pass would have 200, the second 500, the third 347 and then 192 in the last pass?

In [10]: df.join(pd.DataFrame([[1]*x for x in df['months']],
...: columns=['{}/1/2016'.format(i) for i in range(1,13)],
...: index=df.index).fillna(0).mul(df['allot'], axis=0))
...:
...:
Out[10]:
allot months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \
0 200 12 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0
1 500 10 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 500.0 0.0
2 347 6 347.0 347.0 347.0 347.0 347.0 347.0 0.0 0.0 0.0 0.0 0.0
3 192 7 192.0 192.0 192.0 192.0 192.0 192.0 192.0 0.0 0.0 0.0 0.0

12/1/2016
0 200.0
1 0.0
2 0.0
3 0.0

关于python - 使用条件条件与 pandas 一起执行 numpy where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46654205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com