Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Python Pandas - Slice DataFrame based on Another Table's Values to Match to Column Name

I have two dataframes, df_stats and df_ratings.

df_stats looks like this

Fruit Rating_Threshold_Low Rating_Threshold_High
1 Apple 4 7
2 Banana 5 9
3 Kiwi 6 8
question from:https://stackoverflow.com/questions/65909856/python-pandas-slice-dataframe-based-on-another-tables-values-to-match-to-colu

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

1 Answer

0 votes
by (71.8m points)

You could do something like this

sums = []
for i in range(len(df_stats)):
   min_v, max_v = df_stats["Rating_Threshold_Low"].values()[i], df_stats["Rating_Threshold_High"].values()[i]  
   values = []
   for z in range(min_v, max_v+1):
      x = df_ratings[str(z)][i]
      values.append(x)
   sums.append(sum(values))
df_stats["Rating_Threshold_Sum"] = sums

This is really complicated and there is probably a better way to do it but it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...