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
4.1k views
in Technique[技术] by (71.8m points)

python - Find uniqe combinations in DF

I need this code to go over a dictionary (many columns of a dataframe) and find the most unique combination values in the dimensions of a dataframe.

For example: in movies DF, if I send the columns genre and director + the measure Score to the function I'll get the max value from all unique combinations in genre and director:

def bi_robot(dataframe, dimensions, measure):
   t = {}
   for d in dimensions:
      du = dataframe[d].unique()
      t[d] = du[0]
   for v in t:
      key = v
      value = [t[v]]
      dataframe = dataframe.loc[dataframe[key].isin(value)] 
      stat = dataframe[measure].max()
   print('---------------------')
   print(t, end = '')  
   print(' Max:' + "{:.2f}".format(stat))
   print('---------------------')
   return t

df = df_rp.copy(deep = True)
filters = ['genre', 'director']
measure = 'score'
df = bi_robot(df, filters, measure)

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

1 Reply

0 votes
by (71.8m points)
等待大神解答

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

...