简体   繁体   English

Python - 如何根据另一列中的值更改 pandas dataframe 的一列中的值组?

[英]Python - How to change groups of values in one column of pandas dataframe depending on a value in another column?

I have been searching everywhere on stack for this question and answer but I can't seem to find it anywhere.我一直在堆栈上到处寻找这个问题和答案,但我似乎无法在任何地方找到它。

I have a pandas dataframe which looks like the example below:我有一个 pandas dataframe 看起来像下面的例子:

product产品 purchase购买 price价格
credit '信用 '
toy玩具 cash '现金 ' £20 20 英镑
electronics电子产品 cash '现金 ' £50 50 英镑
groceries杂货 cash '现金 ' £80 80 英镑
gaming赌博 cash '现金 ' £30 30 英镑
cash '现金 '
toy玩具 credit '信用 ' £20 20 英镑
electronics电子产品 credit '信用 ' £50 50 英镑
groceries杂货 credit '信用 ' £80 80 英镑
gaming赌博 credit '信用 ' £30 30 英镑
transfer '转移 '
toy玩具 cash '现金 ' £20 20 英镑
electronics电子产品 cash '现金 ' £50 50 英镑
groceries杂货 cash '现金 ' £80 80 英镑
gaming赌博 cash '现金 ' £30 30 英镑

So I tried to make a dataframe above to show you what I mean.所以我试着在上面做一个 dataframe 来告诉你我的意思。 Essentially in the dataframe above I want the values in the 2nd column to be replaced by the 1st corresponding value at the top of each group.基本上在上面的 dataframe 中,我希望将第二列中的值替换为每组顶部的第一个对应值。

Edit: To make it easier to understand I have added symbols for the values I want to change.编辑:为了更容易理解,我为要更改的值添加了符号。 So in the first instance, credit is taken from the product column and this replaces cash for each value in the purchase column until it gets to Cash in the Product column which would then change values in the purchase column for the next 4 items from credit to cash and then it would do the same for transfer.因此,在第一种情况下,信用是从产品列中获取的,这将替换购买列中每个值的现金,直到它到达产品列中的现金,然后将下一个 4 项的购买列中的值从信用更改为现金,然后它会做同样的转移。

So for the first group the value is credit but the second column for the first group of items is “cash” can I create a function which takes the first value at the top of the group and assigns all the cash values for only that group to credit based on the first item in that group?因此,对于第一组,价值是信用,但第一组项目的第二列是“现金”,我可以创建一个 function,它采用组顶部的第一个值并将仅该组的所有现金值分配给基于该组中的第一项的信用?

And then the same for the second group where the first item is cash, I want it to take in the first item of the second group and replace all the credit values for that group to the first item of the second group which in this case would be cash.然后对于第一项是现金的第二组也是如此,我希望它接收第二组的第一项并将该组的所有信用值替换为第二组的第一项,在这种情况下将是现金。

And so on down the list?等等?

Apologies if this is not very clear but if anyone can help solve this I will be extremely grateful.抱歉,如果这不是很清楚,但如果有人可以帮助解决这个问题,我将非常感激。 :) :)

What I would like to see in the output: :)我想在 output 中看到什么::)

product产品 purchase购买 price价格
credit信用
toy玩具 credit信用 £20 20 英镑
electronics电子产品 credit信用 £50 50 英镑
groceries杂货 credit信用 £80 80 英镑
gaming赌博 credit信用 £30 30 英镑
cash现金
toy玩具 cash现金 £20 20 英镑
electronics电子产品 cash现金 £50 50 英镑
groceries杂货 cash现金 £80 80 英镑
gaming赌博 cash现金 £30 30 英镑
transfer转移
toy玩具 transfer转移 £20 20 英镑
electronics电子产品 transfer转移 £50 50 英镑
groceries杂货 transfer转移 £80 80 英镑
gaming赌博 transfer转移 £30 30 英镑

Thank Youuuuu谢谢你

You can iterate through the rows using the iterrows() method.您可以使用 iterrows() 方法遍历行。 From there, you can check if a row's columns are empty and save your group name.从那里,您可以检查一行的列是否为空并保存您的组名。 When you find full rows below, you can write the group name into the appropriate slot.当您在下面找到完整的行时,您可以将组名写入相应的插槽。

temp = None
for i, row in df.iterrows():
    if str(row['purchase']) == 'nan':
        if str(row['product']) != 'nan':
            temp = row['product']
            print(temp)
    elif temp:
        df.iloc[i]['purchase'] = temp

I believe this code should get you what you want.我相信这段代码应该能得到你想要的。

Basically you want to use a group by function to group rows based on values, and use the head function to return the top value of "price" for each group.基本上,您想使用 function 的组来根据值对行进行分组,并使用头部 function 返回每个组的“价格”的最高值。

df = df.groupby(["product", "purchase"]).head(1)
temp = None
for i, row in df.iterrows():
if str(row['purchase']) == 'nan':
if str(row['product']) != 'nan':
temp = row['product']
print(temp)
elif temp:
df.iloc[i]['purchase'] = temp

This method is working, please check your field name "purchase" not "Purchase".此方法有效,请检查您的字段名称“购买”而不是“购买”。 Check your Capital Letter Case检查您的大写字母大小写

Thanks Leon谢谢莱昂

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 pandas 中的另一个列值更改列值 - Change column values depending a another column value in pandas Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 如何使用python将数据框中的一列值更改为另一列值 - How to change values from one column to another in a dataframe using python 如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值? - How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas? Python Pandas:将一列的值检查到另一列 dataframe - Python Pandas: checking value of one column into column of another dataframe 如何根据一列的值更改 DataFrame 的行 - how to change the row of a DataFrame depending on values of one column 如何根据 Pandas 中的另一个 DataFrame 更改 DataFrame 特定列中的值 - How to change values in certain column of DataFrame based on another DataFrame in Pandas dataframe 的列中存在的子集字符串,具体取决于另一列的值 - Pandas - Subsetting strings present in a column of a dataframe, depending on value of another column - Pandas Python pandas 数据框检查一列的值是否在另一个列表中 - Python pandas dataframe check if values of one column is in another list Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM