简体   繁体   English

遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列

[英]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.

相关问题 使用一个 Pandas 数据框填充另一个 Pandas 数据框的新列 - Using one pandas dataframe to populate new column in another pandas dataframe 使用另一个数据框创建熊猫数据框列 - Create pandas dataframe column using another dataframe 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 使用 Python pandas 数据框列作为通过另一列的循环的输入 - Using a Python pandas dataframe column as input to a loop through another column 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 使用if语句针对另一列在pandas数据框中创建新列 - Create new column in pandas dataframe using if statement against another column pandas:通过比较DataFrame的一列的DataFrame行来创建新列 - pandas: Create new column by comparing DataFrame rows of one column of DataFrame 将一个 dataframe 中的一列与另一个 dataframe pandas 中的许多列进行比较 - Compare a column in one dataframe with many columns in another dataframe pandas 循环将创建新的Pandas.DataFrame列 - Loop that will create new Pandas.DataFrame column 比较另一列 dataframe 中一列的值 dataframe - Compare values of one column of dataframe in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM