我的DataFrame中有一个uint64
列,但是当我使用DataFrame.to_dict('record')
将该DataFrame转换为python dict列表时,之前uint64
的{{1}}被神奇地转换了漂浮:
In [24]: mid['bd_id'].head()
Out[24]:
0 0
1 6957860914294
2 7219009614965
3 7602051814214
4 7916807114255
Name: bd_id, dtype: uint64
In [25]: mid.to_dict('record')[2]['bd_id']
Out[25]: 7219009614965.0
In [26]: bd = mid['bd_id']
In [27]: bd.head().to_dict()
Out[27]: {0: 0, 1: 6957860914294, 2: 7219009614965, 3: 7602051814214, 4: 7916807114255}
我怎样才能避免这种奇怪的行为?
奇怪的是,如果我使用to_dict()
而不是to_dict('records')
,则bd_id
列的类型为int:
In [43]: mid.to_dict()['bd_id']
Out[43]:
{0: 0,
1: 6957860914294,
2: 7219009614965,
...
答案 0 :(得分:10)
这是因为另一列中有一个浮点数。更具体地说,values
是使用数据框的object
属性而不是列本身实现的,这实现了"隐式向上转换",在您的情况下将uint64转换为浮动。
如果您想解决此错误,可以将数据帧显式转换为df.astype(object).to_dict('record')[2]['bd_id']
Out[96]: 7602051814214
数据类型:
??
顺便说一句,如果您正在使用IPython,并且想要查看函数在库中的实现方式,可以通过在方法调用结束时放置pd.DataFrame.to_dict??
来使其变为现实。对于 ...
elif orient.lower().startswith('r'):
return [dict((k, v) for k, v in zip(self.columns, row))
for row in self.values]
,我们看到了
for (var intCount = 0; intCount < ds.Tables[0].Rows.Count; intCount++)
{
var dateTime = Convert.ToDateTime(ds.Tables[0].Rows[intCount]["OrderDate"]);
if (!dateTime.Day.ToString().Equals("7") && !dateTime.Month.ToString().Equals("7")) //Replace 7 with user input
{
ds.Tables[0].Rows[intCount].Delete();
}
}
ds.AcceptChanges();
答案 1 :(得分:1)
您可以使用此
from pandas.io.json import dumps
import json
output=json.loads(dumps(mid,double_precision=0))