我想创建一个计算列,其中包含每小时值中的每月值。
我尝试过
df['Monthly'] = df['SpotPrice'].multiply(744)
这个
df['Monthly'] = df['SpotPrice'].multiply(744, level=1)
这个
df['Monthly'] = df['SpotPrice'] * 744
以及其他我从其他SO复制/粘贴的其他一些荒谬的答案,这些答案肯定会被标记为重复的,但无济于事。
我有:
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
..
>>> pandas.__version__
'0.23.0'
已更新
我现有的DataFrame看起来像这样
AvailabilityZone InstanceType ProductDescription SpotPrice Timestamp
40 us-east-2b t2.micro Linux/UNIX 0.003500 2018-07-07 22:09:52+00:00
940 us-east-2c t2.micro Linux/UNIX 0.003500 2018-07-06 22:09:47+00:00
941 us-east-2b t2.micro Linux/UNIX 0.003500 2018-07-06 22:09:47+00:00
942 us-east-2a t2.micro Linux/UNIX 0.003500 2018-07-06 22:09:47+00:00
39 us-east-2c t2.micro Linux/UNIX 0.003500 2018-07-07 22:09:52+00:00
41 us-east-2a t2.micro Linux/UNIX 0.003500 2018-07-07 22:09:52+00:00
115 us-east-2a t2.small Linux/UNIX 0.006900 2018-07-07 21:25:10+00:00
114 us-east-2b t2.small Linux/UNIX 0.006900 2018-07-07 21:25:10+00:00
113 us-east-2c t2.small Linux/UNIX 0.006900 2018-07-07 21:25:10+00:00
945 us-east-2a t2.micro Windows 0.008100 2018-07-06 22:09:38+00:00
44 us-east-2a t2.micro Windows 0.008100 2018-07-07 22:09:39+00:00
43 us-east-2b t2.micro Windows 0.008100 2018-07-07 22:09:39+00:00
42 us-east-2c t2.micro Windows 0.008100 2018-07-07 22:09:39+00:00
944 us-east-2b t2.micro Windows 0.008100 2018-07-06 22:09:38+00:00
943 us-east-2c t2.micro Windows 0.008100 2018-07-06 22:09:38+00:00
877 us-east-2c t2.medium Linux/UNIX 0.013900 2018-07-07 00:33:17+00:00
879 us-east-2a t2.medium Linux/UNIX 0.013900 2018-07-07 00:33:17+00:00
878 us-east-2b t2.medium Linux/UNIX 0.013900 2018-07-07 00:33:17+00:00
103 us-east-2a t2.small Windows 0.015900 2018-07-07 21:26:17+00:00
101 us-east-2c t2.small Windows 0.015900 2018-07-07 21:26:17+00:00
102 us-east-2b t2.small Windows 0.015900 2018-07-07 21:26:17+00:00
998 us-east-2b t2.small Windows 0.015900 2018-07-06 21:25:19+00:00
999 us-east-2a t2.small Windows 0.015900 2018-07-06 21:25:19+00:00
997 us-east-2c t2.small Windows 0.015900 2018-07-06 21:25:19+00:00
511 us-east-2c c4.large Linux/UNIX 0.017200 2018-07-07 11:33:18+00:00
输出通常如下所示:
AvailabilityZone ... Monthly
57 us-east-2a ... 0.0035000.0035000.0035000.0035000.0035000.0035...
56 us-east-2b ... 0.0035000.0035000.0035000.0035000.0035000.0035...
55 us-east-2c ... 0.0035000.0035000.0035000.0035000.0035000.0035...
958 us-east-2a ... 0.0035000.0035000.0035000.0035000.0035000.0035...
957 us-east-2b ... 0.0035000.0035000.0035000.0035000.0035000.0035...
956 us-east-2c ... 0.0035000.0035000.0035000.0035000.0035000.0035...
131 us-east-2a ... 0.0069000.0069000.0069000.0069000.0069000.0069...
129 us-east-2c ... 0.0069000.0069000.0069000.0069000.0069000.0069...
130 us-east-2b ... 0.0069000.0069000.0069000.0069000.0069000.0069...
961 us-east-2a ... 0.0081000.0081000.0081000.0081000.0081000.0081...
959 us-east-2c ... 0.0081000.0081000.0081000.0081000.0081000.0081...
60 us-east-2a ... 0.0081000.0081000.0081000.0081000.0081000.0081...
59 us-east-2b ... 0.0081000.0081000.0081000.0081000.0081000.0081...
58 us-east-2c ... 0.0081000.0081000.0081000.0081000.0081000.0081...
960 us-east-2b ... 0.0081000.0081000.0081000.0081000.0081000.0081...
894 us-east-2b ... 0.0139000.0139000.0139000.0139000.0139000.0139...
895 us-east-2a ... 0.0139000.0139000.0139000.0139000.0139000.0139...
893 us-east-2c ... 0.0139000.0139000.0139000.0139000.0139000.0139...
118 us-east-2b ... 0.0159000.0159000.0159000.0159000.0159000.0159...
117 us-east-2c ... 0.0159000.0159000.0159000.0159000.0159000.0159...
119 us-east-2a ... 0.0159000.0159000.0159000.0159000.0159000.0159...
890 us-east-2c ... 0.0172000.0172000.0172000.0172000.0172000.0172...
527 us-east-2c ... 0.0172000.0172000.0172000.0172000.0172000.0172...
104 us-east-2b ... 0.0177000.0177000.0177000.0177000.0177000.0177...
251 us-east-2a ... 0.0178000.0178000.0178000.0178000.0178000.0178...
74 us-east-2c ... 0.0178000.0178000.0178000.0178000.0178000.0178...
预期输出将是一个带有名为“ Monthly”的新列的框架,该列的SpotPrice值将乘以744。
UPDATE2 这是我完整的脚本
import sys
import boto3
import json
import pandas as pd
region = sys.argv[1]
client = boto3.client('ec2', region)
response = client.describe_spot_price_history()
df = pd.DataFrame(response['SpotPriceHistory'])
df = df.sort_values(by = 'SpotPrice')
df = df.head(50)
df['Monthly'] = df['SpotPrice'] * 744
print(df)
答案 0 :(得分:1)
@Primusa在评论中提供的答案:
df['Monthly'] = df['SpotPrice'].astype(float) * 744