[英]loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas
right now I have two dataframes they look like: 现在我有两个看起来像的数据框:
c = pd.DataFrame({'my_goal':[3, 4, 5, 6, 7],
'low_number': [0,100,1000,2000,3000],
'high_number': [100,1000,2000,3000,4000]})
and 和
a= pd.DataFrame({'a':['a', 'b', 'c', 'd', 'e'],
'Number':[50, 500, 1030, 2005 , 3575]})
what I want to do is if 'Number' falls between the low number and the high number I want it to bring back the value in 'my_goal'. 我想做的是,如果“数字”介于低数字和高数字之间,我希望它带回“ my_goal”中的值。 For example if we look at 'a' it's 'Number is is 100 so I want it to bring back 3. I also want to create a dataframe that contains all the columns from dataframe a and the 'my_goal' column from dataframe c. 例如,如果我们查看“ a”,它的“数字是100,所以我希望它带回3”。我还想创建一个数据框,其中包含数据框a中的所有列和数据框c中的“ my_goal”列。 I want the output to look like: 我希望输出看起来像:
I tried making my high and low numbers into a separate list and running a for loop from that, but all that gives me are 'my_goal' numbers: 我尝试将我的高低数字分成一个单独的列表,然后运行一个for循环,但是所有给我的都是'my_goal'数字:
low_number= 'low_number': [0,100,1000,2000,3000]
for i in a:
if float(i) >= low_number:
a = c['my_goal']
print(a)
You can use pd.cut
, when I see ranges, I first think of pd.cut: 您可以使用pd.cut
,当我看到范围时,我首先想到了pd.cut:
dfa = pd.DataFrame(a)
dfc = pd.DataFrame(c)
dfa['my_goal'] = pd.cut(dfa['Number'],
bins=[0]+dfc['high_number'].tolist(),
labels=dfc['my_goal'])
Output: 输出:
a Number my_goal
0 a 50 3
1 b 500 4
2 c 1030 5
3 d 2005 6
4 e 3575 7
I changed row 4 slightly to include a test case where the condition is not met. 我稍微更改了第4行,以包含一个不满足条件的测试用例。 You can concat a with rows of c where the condition is true. 您可以在条件为真的情况下将a与c行合并。
a= pd.DataFrame({'a':['a', 'b', 'c', 'd', 'e'],'Number':[50, 500, 1030, 1995 , 3575]})
cond= a.Number.between( c.low_number, c.high_number)
pd.concat([a, c.loc[cond, ['my_goal']] ], axis = 1, join = 'inner')
Number a my_goal
0 50 a 3
1 500 b 4
2 1030 c 5
4 3575 e 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.