我的数据框如下:
index name value
0 John Foo
1 John Bar
2 John Bar
3 John Foo
4 Jane Foo
5 Jane Bar
6 Jane Bar
7 John Foo
8 John Bar
我正在尝试将所有出现的John
放在出现该名称的第一组之后,以便使数据看起来像这样:
index name value
0 John Foo
1 John Bar
2 John Bar
3 John Foo
4 Jane Foo
5 Jane Bar
6 Jane Bar
当然,使用df.drop_duplicates(['name'])
每个名称只能保留一行。我知道有多种方法可以解决,但有一种熊猫特定的方法可以删除重复出现在特定值的连续行之后的值吗?
答案 0 :(得分:1)
创建一个帮助列@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,600');
.custom-social-proof {
position: fixed;
display:none;
bottom: 20px;
left: 20px;
z-index: 9999999999999 !important;
font-family: 'Open Sans', sans-serif;
}
.custom-social-proof .custom-notification {
width: 320px;
border: 0;
text-align: left;
z-index: 99999;
box-sizing: border-box;
font-weight: 400;
border-radius: 6px;
box-shadow: 2px 2px 10px 2px rgba(11, 10, 10, 0.2);
background-color: #fff;
position: relative;
cursor: pointer;
}
.custom-social-proof .custom-notification .custom-notification-container {
display: flex !important;
align-items: center;
height: 80px;
}
.custom-social-proof .custom-notification .custom-notification-container .custom-notification-image-wrapper img {
max-height: 75px;
width: 90px;
overflow: hidden;
border-radius: 6px 0 0 6px;
object-fit: cover;
}
.custom-social-proof .custom-notification .custom-notification-container .custom-notification-content-wrapper {
margin: 0;
height: 100%;
color: gray;
padding-left: 20px;
padding-right: 20px;
border-radius: 0 6px 6px 0;
flex: 1;
display: flex !important;
flex-direction: column;
justify-content: center;
}
.custom-social-proof .custom-notification .custom-notification-container .custom-notification-content-wrapper .custom-notification-content {
font-family: inherit !important;
margin: 0 !important;
padding: 0 !important;
font-size: 14px;
line-height: 16px;
}
.custom-social-proof .custom-notification .custom-notification-container .custom-notification-content-wrapper .custom-notification-content small {
margin-top: 3px !important;
display: block !important;
font-size: 12px !important;
opacity: 0.8;
}
.custom-social-proof .custom-notification .custom-close {
position: absolute;
top: 8px;
right: 8px;
height: 12px;
width: 12px;
cursor: pointer;
transition: 0.2s ease-in-out;
transform: rotate(45deg);
opacity: 0;
}
.custom-social-proof .custom-notification .custom-close::before {
content: "";
display: block;
width: 100%;
height: 2px;
background-color: gray;
position: absolute;
left: 0;
top: 5px;
}
.custom-social-proof .custom-notification .custom-close::after {
content: "";
display: block;
height: 100%;
width: 2px;
background-color: gray;
position: absolute;
left: 5px;
top: 0;
}
.custom-social-proof .custom-notification:hover .custom-close {
opacity: 1;
}
,该列定义<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="custom-social-proof">
<div class="custom-notification">
<div class="custom-notification-container">
<div class="custom-notification-image-wrapper">
<img src="https://tidings.today/wp-content/uploads/2018/08/tidings-today-logo-fav.png">
</div>
<div class="custom-notification-content-wrapper">
<p id="random" class="custom-notification-content"></p>
</div>
</div>
<div class="custom-close"></div>
</div>
</section>
的连续组,然后仅保留每个名称的第一组:(为便于说明,请保留在帮助列中)
'ng'