我正在尝试将值从数据框中的一列传输到另一列,具有多个条件且不在同一行中。
从列“BEGUZ_H”和“ENDUZ_H”到列“BEGUZ”和“ENDUZ”的值,
如果“PERNR_H”、“WORKDATE_H”、“AWART_H”列中的值
与“PERNR”、“WORKDATE”、“AWART”列中的值相等。
data = {'PERNR': [138888, 138432, 138432, 138437, 138437, 126162, 222829, 234740],
'WORKDATE': ['2024-01-25', '2024-01-10', '2024-01-09', '2024-01-26', '2024-01-24', '2024-01-29', '2024-01-15', '2024-01-09'],
'AWART': [2235, 2235, 2235, 2235, 2235, 2235, 2235, 2235],
'BEGUZ': ['00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00'],
'ENDUZ': ['00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00'],
'PERNR_H': [0, 0, 126162, 222829, 234740, 0, 0, 138888],
'WORKDATE_H': ['NaN', 'NaN', '2024-01-29', '2024-01-15', '2024-01-09', 'NaN', 'NaN', '2024-01-25'],
'AWART_H': [2235, 2235, 2235, 2235, 2235, 2235, 2235, 2235],
'BEGUZ_H': ['NaN', 'NaN', '07:59:00', '07:50:00', '07:03:00', 'NaN', 'NaN', '07:45:00'],
'ENDUZ_H': ['NaN', 'NaN', '17:30:00', '17:35:00', '18:28:00', 'NaN', 'NaN', '18:59:00']}
df = pd.DataFrame(data)
df
Out[11]:
PERNR WORKDATE AWART BEGUZ ENDUZ PERNR_H WORKDATE_H AWART_H BEGUZ_H ENDUZ_H
0 138888 2024-01-25 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
1 138432 2024-01-10 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
2 138432 2024-01-09 2235 00:00:00 00:00:00 126162 2024-01-29 2235 07:59:00 17:30:00
3 138437 2024-01-26 2235 00:00:00 00:00:00 222829 2024-01-15 2235 07:50:00 17:35:00
4 138437 2024-01-24 2235 00:00:00 00:00:00 234740 2024-01-09 2235 07:03:00 18:28:00
5 126162 2024-01-29 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
6 222829 2024-01-15 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
7 234740 2024-01-09 2235 00:00:00 00:00:00 138888 2024-01-25 2235 07:45:00 18:59:00
我的方法是 'np.where' ,但这只适用于相同的行。
np.where((df['PERNR']==df['PERNR_H'])&(df['WORKDATE']==df['WORKDATE_H'])&(df['AWART']==df['AWART_H']),df['BEGUZ_H'], df['ENDUZ_H'])
我期望:
PERNR WORKDATE AWART BEGUZ ENDUZ PERNR_H WORKDATE_H AWART_H BEGUZ_H ENDUZ_H
0 138888 2024-01-25 2235 07:45:00 18:59:00 0 NaN 2235 NaN NaN
1 138432 2024-01-10 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
2 138432 2024-01-09 2235 00:00:00 00:00:00 126162 2024-01-29 2235 07:59:00 17:30:00
3 138437 2024-01-26 2235 00:00:00 00:00:00 222829 2024-01-15 2235 07:50:00 17:35:00
4 138437 2024-01-24 2235 00:00:00 00:00:00 234740 2024-01-09 2235 07:03:00 18:28:00
5 126162 2024-01-29 2235 07:59:00 17:30:00 0 NaN 2235 NaN NaN
6 222829 2024-01-15 2235 07:50:00 17:35:00 0 NaN 2235 NaN NaN
7 234740 2024-01-09 2235 07:03:00 18:28:00 138888 2024-01-25 2235 07:45:00 18:59:00
根据列名称将数据框分成两部分,然后
merge
将它们放回到 'PERNR' , 'WORKDATE' , 'AWART'
和 'PERNR_H' , 'WORKDATE_H' , 'AWART_H'
列上。然后你可以简单地替换这些值。
df_h = df.filter(regex=r"_H$")
df = df.filter(regex=r"^(?!.*?_H)")
df = df.merge(
df_h,
left_on=["PERNR", "WORKDATE", "AWART"],
right_on=["PERNR_H", "WORKDATE_H", "AWART_H"],
how="left",
)
df.loc[df["BEGUZ_H"].notna(), "BEGUZ"] = df["BEGUZ_H"]
df.loc[df["ENDUZ_H"].notna(), "ENDUZ"] = df["ENDUZ_H"]
df = pd.concat([df.filter(regex=r"^(?!.*?_H)"), df_h], axis=1)
PERNR WORKDATE AWART BEGUZ ENDUZ PERNR_H WORKDATE_H AWART_H BEGUZ_H ENDUZ_H
0 138888 2024-01-25 2235 07:45:00 18:59:00 0 NaN 2235 NaN NaN
1 138432 2024-01-10 2235 00:00:00 00:00:00 0 NaN 2235 NaN NaN
2 138432 2024-01-09 2235 00:00:00 00:00:00 126162 2024-01-29 2235 07:59:00 17:30:00
3 138437 2024-01-26 2235 00:00:00 00:00:00 222829 2024-01-15 2235 07:50:00 17:35:00
4 138437 2024-01-24 2235 00:00:00 00:00:00 234740 2024-01-09 2235 07:03:00 18:28:00
5 126162 2024-01-29 2235 07:59:00 17:30:00 0 NaN 2235 NaN NaN
6 222829 2024-01-15 2235 07:50:00 17:35:00 0 NaN 2235 NaN NaN
7 234740 2024-01-09 2235 07:03:00 18:28:00 138888 2024-01-25 2235 07:45:00 18:59:00