关于pandas多重条件判断的问题

我有一组数据,我想用pandas多个条件判断后按如下格式输出,应该怎么做?
输出如下:

Serial Number    Test Time    Test Steps    Test Condition    Ant    Freq    Value
A1234567    2023/3/17 16:55    step_1    cnd_1    1    [2412,2442,2472]    [-73,-73,-73]
A1234567    2023/3/17 16:55    step_1    cnd_2    1    [2412,2442,2472]    [-73,-74,-73]
A1234567    2023/3/17 16:55    step_1    cnd_3    1    [2412,2442,2472]    [-73,-73,-72]
A1234567    2023/3/17 16:55    step_1    cnd_1    2    [2412,2442,2472]    [-75,-75,-75]
A1234567    2023/3/17 16:55    step_1    cnd_2    2    [2412,2442,2472]    [-75,-74,-75]
A1234567    2023/3/17 16:55    step_1    cnd_3    2    [2412,2442,2472]    [-74,-75,-75]
A1234567    2023/3/17 16:42    step_1    cnd_1    1    [2412,2442,2472]    [-73,-73,-72]
A1234567    2023/3/17 16:42    step_1    cnd_2    1    [2412,2442,2472]    [-73,-73,-73]
A1234567    2023/3/17 16:42    step_1    cnd_3    1    [2412,2442,2472]    [-73,-73,-72]
A1234567    2023/3/17 16:42    step_1    cnd_1    2    [2412,2442,2472]    [-75,-74,-75]
A1234567    2023/3/17 16:42    step_1    cnd_2    2    [2412,2442,2472]    [-74,-75,-74]
A1234567    2023/3/17 16:42    step_1    cnd_3    2    [2412,2442,2472]    [-73,-73,-74]
B1234567    2023/3/11 16:04    step_2    cnd_1    1    [2412,2442,2472]    [-71,-71,-72]
B1234567    2023/3/11 16:04    step_2    cnd_2    1    [2412,2442,2472]    [-71,-72,-72]
B1234567    2023/3/11 16:04    step_2    cnd_3    1    [2412,2442,2472]    [-71,-71,-70]
B1234567    2023/3/11 16:04    step_2    cnd_1    2    [2412,2442,2472]    [-74,-75,-74]
B1234567    2023/3/11 16:04    step_2    cnd_2    2    [2412,2442,2472]    [-73,-74,-74]
B1234567    2023/3/11 16:04    step_2    cnd_3    2    [2412,2442,2472]    [-74,-74,-74]
B1234567    2023/3/11 16:16    step_2    cnd_1    1    [2412,2442,2472]    [-73,-73,-72]
B1234567    2023/3/11 16:16    step_2    cnd_2    1    [2412,2442,2472]    [-73,-73,-73]
B1234567    2023/3/11 16:16    step_2    cnd_3    1    [2412,2442,2472]    [-73,-72,-71]
B1234567    2023/3/11 16:16    step_2    cnd_1    2    [2412,2442,2472]    [-73,-74,-72]
B1234567    2023/3/11 16:16    step_2    cnd_2    2    [2412,2442,2472]    [-73,-73,-72]
B1234567    2023/3/11 16:16    step_2    cnd_3    2    [2412,2442,2472]    [-71,-74,-75]

数据如下

Serial Number    Test Time    Test Steps    Test Condition    Ant    Freq    Value
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2442    -73
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2472    -73
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2442    -74
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2472    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2442    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2472    -72
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2412    -75
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2442    -75
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2472    -75
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2412    -75
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2442    -74
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2472    -75
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2412    -74
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2442    -75
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2472    -75
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2412    -72
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2472    -72
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2472    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2472    -72
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2412    -75
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2442    -74
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2472    -75
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2412    -74
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2442    -75
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2472    -74
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2442    -71
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2472    -72
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2442    -72
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2472    -72
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2442    -71
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2472    -70
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2412    -74
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2442    -75
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2412    -73
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2442    -74
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2412    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2442    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2472    -74
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2472    -73
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2442    -72
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2472    -71
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2442    -74
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2412    -71
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2442    -74
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2472    -75
Jason990420
最佳答案

pandas.DataFrame.groupby

dropna: bool, default True

If True, and if group keys contain NA values, NA values together with row/column will be dropped. If False, NA values will also be treated as the key in groups.

new_df = df.groupby(['Serial Number', 'Vendor', 'Test Time', 'Test Steps', 'Test Condition', 'Ant'], dropna=False).agg(tuple).applymap(list).reset_index()
6个月前 评论
jwwlchen (楼主) 6个月前
讨论数量: 6
Jason990420
import io
import pandas as pd

data = """
Serial Number    Test Time    Test Steps    Test Condition    Ant    Freq    Value
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2442    -73
A1234567    2023/3/17 16:55    step_1    cnd_1    1    2472    -73
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2442    -74
A1234567    2023/3/17 16:55    step_1    cnd_2    1    2472    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2412    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2442    -73
A1234567    2023/3/17 16:55    step_1    cnd_3    1    2472    -72
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2412    -75
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2442    -75
A1234567    2023/3/17 16:55    step_1    cnd_1    2    2472    -75
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2412    -75
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2442    -74
A1234567    2023/3/17 16:55    step_1    cnd_2    2    2472    -75
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2412    -74
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2442    -75
A1234567    2023/3/17 16:55    step_1    cnd_3    2    2472    -75
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2412    -72
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_1    1    2472    -72
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_2    1    2472    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    1    2472    -72
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2412    -75
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2442    -74
A1234567    2023/3/17 16:42    step_1    cnd_1    2    2472    -75
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2412    -74
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2442    -75
A1234567    2023/3/17 16:42    step_1    cnd_2    2    2472    -74
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2412    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2442    -73
A1234567    2023/3/17 16:42    step_1    cnd_3    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2442    -71
B1234567    2023/3/11 16:04    step_2    cnd_1    1    2472    -72
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2442    -72
B1234567    2023/3/11 16:04    step_2    cnd_2    1    2472    -72
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2412    -71
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2442    -71
B1234567    2023/3/11 16:04    step_2    cnd_3    1    2472    -70
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2412    -74
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2442    -75
B1234567    2023/3/11 16:04    step_2    cnd_1    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2412    -73
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2442    -74
B1234567    2023/3/11 16:04    step_2    cnd_2    2    2472    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2412    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2442    -74
B1234567    2023/3/11 16:04    step_2    cnd_3    2    2472    -74
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    1    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    1    2472    -73
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2442    -72
B1234567    2023/3/11 16:16    step_2    cnd_3    1    2472    -71
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2442    -74
B1234567    2023/3/11 16:16    step_2    cnd_1    2    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2412    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2442    -73
B1234567    2023/3/11 16:16    step_2    cnd_2    2    2472    -72
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2412    -71
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2442    -74
B1234567    2023/3/11 16:16    step_2    cnd_3    2    2472    -75
""".strip()

df = pd.read_csv(io.StringIO(data), sep='\s{2,}', header=0, engine='python')
new_df = df.groupby(['Serial Number', 'Test Time', 'Test Steps', 'Test Condition', 'Ant']).agg(tuple).map(list).reset_index()
6个月前 评论

运行报错了, :grin:

new_df = df.groupby([‘Serial Number’, ‘Test Time’, ‘Test Steps’, ‘Test Condition’, ‘Ant’]).agg(tuple).map(list).reset_index()
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/generic.py”, line 5989, in getattr
return object.getattribute(self, name)

AttributeError: ‘DataFrame’ object has no attribute ‘map’

6个月前 评论
Jason990420

What version of your installed pandas ?

Try applymap

Deprecated since version 2.1.0: DataFrame.applymap has been deprecated. Use DataFrame.map instead.

6个月前 评论

Thanks, it’s ok with applymap.
but it has another issue. while i add a new column Vendor and has some null value, it’ll shikp these rows.
It should print 24x records but only 23x

code:

csv = r'Sample.csv'

df = pd.read_csv(csv, sep=',', header=0, engine='python')
new_df = df.groupby(['Serial Number', 'Vendor', 'Test Time', 'Test Steps', 'Test Condition', 'Ant']).agg(tuple).applymap(list).reset_index()
Serial Number,Vendor,Test Time,Test Steps,Test Condition,Ant,Freq,Value
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,1,2412,-73
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,1,2442,-73
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,1,2472,-73
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,1,2412,-73
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,1,2442,-74
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,1,2472,-73
A1234567,,2023/3/17 16:55,step_1,cnd_3,1,2412,-73
A1234567,,2023/3/17 16:55,step_1,cnd_3,1,2442,-73
A1234567,,2023/3/17 16:55,step_1,cnd_3,1,2472,-72
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,2,2412,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,2,2442,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_1,2,2472,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,2,2412,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,2,2442,-74
A1234567,AMP,2023/3/17 16:55,step_1,cnd_2,2,2472,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_3,2,2412,-74
A1234567,AMP,2023/3/17 16:55,step_1,cnd_3,2,2442,-75
A1234567,AMP,2023/3/17 16:55,step_1,cnd_3,2,2472,-75
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,1,2412,-72
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,1,2442,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,1,2472,-72
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,1,2412,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,1,2442,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,1,2472,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,1,2412,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,1,2442,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,1,2472,-72
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,2,2412,-75
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,2,2442,-74
A1234567,AMP,2023/3/17 16:42,step_1,cnd_1,2,2472,-75
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,2,2412,-74
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,2,2442,-75
A1234567,AMP,2023/3/17 16:42,step_1,cnd_2,2,2472,-74
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,2,2412,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,2,2442,-73
A1234567,AMP,2023/3/17 16:42,step_1,cnd_3,2,2472,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,1,2412,-71
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,1,2442,-71
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,1,2472,-72
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,1,2412,-71
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,1,2442,-72
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,1,2472,-72
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,1,2412,-71
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,1,2442,-71
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,1,2472,-70
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,2,2412,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,2,2442,-75
B1234567,SW,2023/3/11 16:04,step_2,cnd_1,2,2472,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,2,2412,-73
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,2,2442,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_2,2,2472,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,2,2412,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,2,2442,-74
B1234567,SW,2023/3/11 16:04,step_2,cnd_3,2,2472,-74
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,1,2412,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,1,2442,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,1,2472,-72
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,1,2412,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,1,2442,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,1,2472,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,1,2412,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,1,2442,-72
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,1,2472,-71
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,2,2412,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,2,2442,-74
B1234567,SW,2023/3/11 16:16,step_2,cnd_1,2,2472,-72
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,2,2412,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,2,2442,-73
B1234567,SW,2023/3/11 16:16,step_2,cnd_2,2,2472,-72
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,2,2412,-71
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,2,2442,-74
B1234567,SW,2023/3/11 16:16,step_2,cnd_3,2,2472,-75
6个月前 评论
Jason990420

pandas.DataFrame.groupby

dropna: bool, default True

If True, and if group keys contain NA values, NA values together with row/column will be dropped. If False, NA values will also be treated as the key in groups.

new_df = df.groupby(['Serial Number', 'Vendor', 'Test Time', 'Test Steps', 'Test Condition', 'Ant'], dropna=False).agg(tuple).applymap(list).reset_index()
6个月前 评论
jwwlchen (楼主) 6个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!