[英]pandas loc does not adds value to dataframe
所以我想在 pandas dataframe 中的特定位置的現有值中添加一個值。我嘗試如下:
import calendar
for index,row in data.iterrows():
print(index)
if row.mask:
date = calendar.monthrange(int(row.year), int(row.month))
date = pd.to_datetime(str(int(row.year))+'-'+str(int(row.month))+'-'+str(date[1]))
diff = data.real - data.fiction
df.loc[df.date==date, 'zeta']+=diff
但是最后這個操作值與之前的值相同,我不明白為什么我使用 loc 指向特定的位置並且可以更改值。 但是我的不起作用。
數據樣本:
所以這里涉及到兩個數據框。 一個是 df,它看起來像:
{'date': [datetime.date(2018, 7, 1),
datetime.date(2018, 7, 2),
datetime.date(2018, 7, 3),
datetime.date(2018, 7, 4),
datetime.date(2018, 7, 5),
datetime.date(2018, 7, 6),
datetime.date(2018, 7, 7),
datetime.date(2018, 7, 8),
datetime.date(2018, 7, 9),
datetime.date(2018, 7, 10)],
'alpha': [899.8399999999998,
804.2400000000001,
824.6400000000001,
903.7599999999999,
761.2900000000001,
766.7999999999998,
765.0699999999998,
882.8600000000001,
741.8199999999999,
729.6600000000001],
'beta': [660.24,
514.87,
456.6600000000001,
490.29,
469.83,
506.4,
571.65,
651.18,
545.5,
544.99],
'gamma': [1555.5299999999988,
1512.829999999999,
1507.4699999999991,
1491.1799999999994,
1019.4199999999994,
650.0699999999995,
674.7599999999999,
676.0899999999992,
464.05999999999966,
455.03000000000003],
'delta': [178.02,
150.75,
136.14999999999998,
147.51999999999998,
160.93000000000004,
131.96999999999997,
117.31,
131.88,
160.57000000000008,
158.73999999999998],
'epsilon': [0.0,
375.7099915,
464.85501100000005,
464.8450012,
484.63500980000003,
514.664978,
471.16000369999995,
459.8599853999999,
461.4349976,
441.9400024],
'zeta': [282.9800053,
156.5300011,
109.93999609999999,
83.86999995,
168.62735590000003,
170.31219380000002,
73.63714508,
119.776293,
179.14328830000002,
446.6358328],
'total': [3576.6100052999986,
3514.929992599999,
3499.7150070999996,
3581.4650011499994,
3064.7323656999997,
2740.2171717999995,
2673.5871487799996,
2921.646278399999,
2552.5282859,
2776.9958352000003]}
和看起來像的數據:
{'month': [1, 2, 3, 4, 5, 6, 7, 8],
'year': [2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020],
'fiction': [4904.049999999999,
5098.29,
8582.139999999998,
13712.130000000001,
20505.370000000003,
3629.21,
0.0,
0.0],
'real': [14528.33,
12592.45,
8582.14,
13712.12,
20505.4,
19356.6,
18205.0,
13028.29],
'mask': [True, True, False, False, False, True, True, True]}
掩碼計算如下:
(data.real - data.fiction).map(int).map(bool)
你有兩個錯誤。
mask 是一個內置的 function, if row.mask
在任何情況下都會返回 True。 因為無論您希望它存在與否,function 都會存在,所以實際上沒有使用基於它的檢查。 我在 if 語句中添加了一個 print 語句。 只是為了確保,嘗試將它添加到您的 if 塊中,您就會知道其中的區別。 (以防萬一你必須那樣命名,雖然我想不出你必須像這樣選擇內置名稱 function、go 的情況: if row['mask']
and don't test pandas' intelligence .)
您可能會收到一些錯誤消息。 您很有可能希望對特定行有所不同(如果沒有,它會在循環整個數據幀時打上一個很大的問號),因此您可能希望使用row.real - row.fiction
而不是data.real - data.fiction
。 我按預期進行了這兩個更改和 dataframe 更改。 這是您可能想要插入的更改代碼
import calendar
for index,row in data.iterrows():
if row.maskr:
print('Going for',index)
date = calendar.monthrange(int(row.year), int(row.month))
date = pd.to_datetime(str(int(row.year))+'-'+str(int(row.month))+'-'+str(date[1]))
diff = row.real - row.fiction
df.loc[df.date==date, 'zeta']+=diff
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.