首页 > 解决方案 > 需要在现有数据框的一列中创建仅具有奇数个值的新数据框

问题描述

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

df = pd.read_excel ('the sample sheet i have attached')

indexNames = df[ (df['counter'] != -1) & (df['counter'] != 1) & (df['id'] >= "Fri Jul 10 19:33:12 GMT+05:30 2020")  ].index
dff.drop(indexNames , inplace=True)

v = df.employee_id.value_counts()

new_df= df[df.employee_id.isin(v.index[v.eq(1)])]
new_df

我希望 new_df 数据框仅包含已登录但未注销的员工的数据。在附加的 excel 数据表中,如果计数器 = -1,那么我假设注销,如果计数器 = 1,我假设工作表的登录链接“https://drive.google.com/file/d/1E9hivsHzc9lc_IQG0mWf36fc8F4HgPDm/view?usp=sharing "

标签: pythonpandasdata-science

解决方案


我暂时没有过滤器,但这里有一种方法可以获取没有计数器记录 = -1 的员工的记录

import pandas as pd
logins = {'EmployeeId': [22, 22, 22, 22, 67, 67, 67],
    'Counter': [1, 2, 3, -1, 1, 2, 3] }

df = pd.DataFrame(logins)
df2 = df.loc[df['Counter'] == -1]
print(df[~df['EmployeeId'].isin(df2['EmployeeId'])])

推荐阅读