我有一个数据框,其中一个col int一个col浮动:
.menu-device {
position: relative;
width: 100%;
height: 95px;
background-color: #FFF;
-webkit-box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
z-index: 1000;
}
.menu-device .menu__button {
text-align: center;
margin-top: 37px;
width: 105px;
height: 58px;
float: right;
}
.menu-device .menu-device__menu {
width: 282px;
border: 1px solid #ddd;
display: none;
position: absolute;
top: 95px;
right: 0;
box-shadow: -3px 0 10px #ababab inset;
}
.menu-device .menu-device__menu .menu-device__item {
background-image: url("../../img/menu_icon.png");
background-position: right center;
background-repeat: no-repeat;
position: relative;
line-height: 35px;
padding-right: 30px;
border-bottom: 1px solid #e5e5e5;
background-color: #FFF;
width: 252px;
height: 35px;
float: right;
text-align: right;
}
.menu-device .menu-device__menu .menu-device__item .menu__link {
color: #494949;
text-decoration: none;
font-size: 16px;
}
.menu-device .menu-device__menu .menu-device__item .submenu {
background-color: #FFF;
position: absolute;
left: 0;
top: 35px;
display: none;
}
.menu-device .menu-device__menu .menu-device__item .submenu .submenu__item {
line-height: 35px;
padding-right: 30px;
background-color: #FFF;
width: 252px;
height: 35px;
float: right;
text-align: right;
}
.menu-device .menu-device__menu .menu-device__item .submenu .submenu__item .submenu__link {
font-size: 14px;
text-decoration: none;
color: #494949;
}
.menu-device .menu-device__menu .menu-device__item .submenu .submenu__item:hover {
background-color: #a90027;
}
.menu-device .menu-device__menu .menu-device__item .submenu .submenu__item:hover .submenu__link {
color: #fff;
}
.menu-device .menu-device__menu .menu-device__item:nth-child(1),
.menu-device .menu-device__menu .menu-device__item:nth-child(3),
.menu-device .menu-device__menu .menu-device__item:nth-child(6) {
background-image: none;
}
.menu-device .menu-device__menu .menu-device__item:hover {
background-color: #a90027;
}
.menu-device .menu-device__menu .menu-device__item:hover .menu__link {
color: #fff;
}
.menu-device .menu__logo {
position: absolute;
float: left;
width: 135px;
margin-left: 91px;
z-index: 2000;
}
.menu-device .menu__logo img {
margin-top: -20px;
max-width: 100%;
}
我想要一个像 <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<nav class="menu-device">
<div class="menu__button">
<a class="menu__pic" href="#"><img src="img/responsive_nav.png" alt="expand"></a>
<ul class="menu-device__menu">
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a></li>
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a>
<ul class="submenu">
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
</ul>
</li>
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a></li>
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a>
<ul class="submenu">
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
</ul>
</li>
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a>
<ul class="submenu">
<li class="submenu__item"><a href="" class="submenu__link">Menu Item</a></li>
</ul>
</li>
<li class="menu-device__item"><a href="#" class="menu__link">Menu Item</a></li>
</ul>
</div>
</nav>
df
# a b
# 0 3 42.00
# 1 2 3.14
df.dtypes
# a int64
# b float64
# dtype: object
但是df.to_dict(orient='records')
为df.to_dict(orient='records')
[{'a': 3.0, 'b': 42.0}, {'a': 2.0, 'b': 3.1400000000000001}]
,而不是作为浮动
答案 0 :(得分:9)
目前(从Pandas版本0.18开始),df.to_dict('records')
访问NumPy数组df.values
。此属性将int
列的dtype向上转换为float
,以便阵列可以具有单个公共dtype。在此之后,没有希望返回所需的结果 - 所有的整数都已转换为浮点数。
相反,在ayhan's和Tom Augspurger's suggestion的基础上,您可以使用列表和词典理解:
import pandas as pd
df = pd.DataFrame({'a':[3,2], 'b':[42.0,3.14]})
result = [{col:getattr(row, col) for col in df} for row in df.itertuples()]
print(result)
# [{'a': 3, 'b': 42.0}, {'a': 2, 'b': 3.1400000000000001}]
答案 1 :(得分:1)
另一个可怕的解决方法是(暂时)添加非数字列,例如从:
开始df = pd.DataFrame([[1, 2.4], [3, 4.0]], columns='a b'.split())
然后df.to_dict(orient='record')
促进浮动,但如果你这样做:
df['foo'] = 'bar'
[{k: v for (k, v) in row.items() if k != 'foo'} for row in df.to_dict(orient='record')]
保留原始类型。我注意到df.reindex()
行为相似,如Pandas gotchas中所述,但除非您填写非零值,否则无法解决问题,例如fill_value=0