尝试按值从数据框中进行选择,实际上是按最小值进行选择并执行以下操作:
results[results['d1']==min_overall['d1']['cao3']]
上面给了我错误信息,比如diff类型的比较。
然后我检查了以下内容:
results['d1'].dtypes : float64
min_overall['d1'].dtypes : object
感到奇怪的是:(' cao3'是其中之一)
days=[1,2,5,10,20,50,100]
stock_analysis=pd.DataFrame(columns=['d'+str(d) for d in days],dtype=np.float64)
results=pd.concat([stockpicks,stock_analysis],axis=1)
results_typed=results.groupby('type')
min_overall=results_typed.min()
min_overall是根据groupby的结果计算的。但是' d1'列从float64转换为object。
为什么呢?我怎么能克服这个?
编辑:此处为结果格式
date type stocknum price pe d1 d2 d5 \
0 2014-02-17 cao3 0326.HK 0.20 20.00 95 95 95
1 2014-02-17 cao3 0536.HK 2.56 25.60 99.21875 100 97.65625
2 2014-02-17 cao3 0595.HK 0.97 48.50 100 107.2165 104.1237
3 2014-02-17 cao3 0698.HK 0.95 15.83 102.1053 97.89474 97.89474
4 2014-02-17 cao3 0759.HK 3.25 108.33 95.38462 94.15385 92
d10
0 90
1 89.0625
2 93.81443
3 105.2632
4 90.15385
[5 rows x 9 columns]
下面添加了更多代码和输出:
df=pd.read_csv(logfile,header=None,names=['date','type','stocknum','price','pe'])
stockpicks=df.drop_duplicates()
days=[1,2,5,10,20,50,100]
stock_analysis=pd.DataFrame(columns=['d'+str(d) for d in days],dtype=np.float64)
results=pd.concat([stockpicks,stock_analysis],axis=1)
results_typed=results.groupby('type')
max_overall=results_typed.max()
min_overall=results_typed.min()
print(results['d2'])
print(results.dtypes)
print(min_overall['d2'])
print(min_overall.dtypes)
print(results[results['d1']==min_overall['d1']['cao3']])
和输出:你可以看到所有d列都是float64,但是在groupby min之后成为对象。
0 95.000000
1 99.218750
2 100.000000
3 102.105263
4 95.384615
5 98.400000
6 96.250000
7 100.000000
8 100.000000
9 96.969697
10 101.675978
11 98.953975
12 94.594595
13 96.969697
14 101.882845
...
4078 101.590106
4079 98.083067
4080 100.341297
4081 97.805643
4082 103.225806
4083 99.137931
4084 102.302632
4085 102.941176
4086 98.083067
4087 104.246285
4088 102.967359
4089 101.224490
4090 99.502488
4091 99.789474
4092 104.380952
Name: d1, Length: 3739, dtype: float64
date object
type object
stocknum object
price float64
pe float64
d1 float64
d2 float64
d5 float64
d10 float64
d20 float64
d50 float64
d100 float64
dtype: object
type
bbom 4.01267828
bbos 4.01267828
cao3 4.01267828
maos 4.01267828
rscp 78.6096256
Name: d2, dtype: object
date object
stocknum object
price object
pe object
d1 object
d2 object
d5 object
d10 object
d20 object
d50 object
d100 object
dtype: object