为什么简单的列乘法在Python 3.5、0.23中不起作用?
我想创建一个计算列,从每小时值中提取每月值 我试过这个为什么简单的列乘法在Python 3.5、0.23中不起作用?,python,pandas,dataframe,Python,Pandas,Dataframe,我想创建一个计算列,从每小时值中提取每月值 我试过这个 df['Monthly']=df['SpotPrice'].乘以(744) 这个 df['Monthly']=df['SpotPrice'].乘以(744,级别=1) 这个 df['Monthly']=df['SpotPrice']*744 还有其他一些荒谬的变化,我复制/粘贴了其他的答案,这些答案肯定会被标记为重复的,但没有任何效果 我有: Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 0
df['Monthly']=df['SpotPrice'].乘以(744)
这个
df['Monthly']=df['SpotPrice'].乘以(744,级别=1)
这个
df['Monthly']=df['SpotPrice']*744
还有其他一些荒谬的变化,我复制/粘贴了其他的答案,这些答案肯定会被标记为重复的,但没有任何效果
我有:
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'
更新
我现有的数据框架如下所示
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。
更新2
这是我的完整剧本
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)
@Primusa评论中提供的答案:
df['Monthly']=df['SpotPrice'].aType(float)*744
“不工作”并不能告诉我们多少。您的示例是做什么的?您希望它们做什么?@JohnGordon刚刚添加了。@ScottBoston添加了输入和实际与预期输出SpotPrice是str而不是数字类型。df.info()返回什么?df['Monthly']=df['SpotPrice'].astype(float)*744
正如Anton vBR在评论中提到的那样,虽然这会起作用,但在导入时清理数据被认为是最佳做法,而不是在使用时。如果您反复使用此专栏,则必须记住在使用时强制使用,这将是一件恼人的事情,而且可能是危险的。(危险是因为0.250.25
可能会导致崩溃,2525
可能不会。)@DSM“导入时清理”在代码中是什么样子?