Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

python - Groupby and lists category

I have the follwing DataFrame

import pandas as pd

data = {"hours": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"values": [0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1]}
df = pd.DataFrame(data)

I have been trying to add an extra column to df including the values by groupby values and the follwing list:

[2, 4, 6, 8, 10, 16, 18, 21, 23]

this list represents hours after which the gruoping should be conducted. E.g. in the new column category it gives 1 for those values between 2 and 4 gives 1 and else where gives 0 and for hours between 6 and 8 gives 2 where the values are 1 and else where 0 and so on.. I tried the following:

df.groupby(["values", "hours"])

and I could not come forward with it.

The expected result looks like:

enter image description here

question from:https://stackoverflow.com/questions/66060998/groupby-and-lists-category

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated to answer question. You'd have to create individual queries (as below). This should work for the specific ranges

df['category'] = 0
df.loc[(df['hours'] >= 2) & (df['hours'] <= 4), 'category'] = df['values']
df.loc[(df['hours'] >= 6) & (df['hours'] <= 8), 'category'] = df['values'] * 2
df.loc[df['hours'] == 10, 'category'] = df['values'] * 3
df.loc[(df['hours'] >= 16) & (df['hours'] <= 18), 'category'] = df['values'] * 4
df.loc[(df['hours'] >= 21) & (df['hours'] <= 23), 'category'] = df['values'] * 5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...