简体   繁体   English

在 pandas 的同一列中将值从一行拆分到其他行

[英]Splitting value from one row to other rows in the same column in pandas

I have a question regarding to pandas我有一个关于 pandas 的问题

I have got a DataFrame df我有一个 DataFrame df

TaskId任务 ID UserId用户身份 Hours小时
123456 123456 123456 123456 19 19
123456 123456 123456 123456 NaN
123456 123456 123456 123456 NaN
123456 123456 123456 123456 NaN
654321 654321 654321 654321 10 10

Now I want to split the 19 from the first row into equal amounts where the TaskId and UserId is the same 19 / 4 = 4.75现在我想将第一行中的 19 分成相等的数量,其中 TaskId 和 UserId 相同 19 / 4 = 4.75

This is what I would like to receive这是我想收到的

TaskId任务 ID UserId用户身份 Hours小时
123456 123456 123456 123456 4.75 4.75
123456 123456 123456 123456 4.75 4.75
123456 123456 123456 123456 4.75 4.75
123456 123456 123456 123456 4.75 4.75
... ... ... ... ... ...

I couldn't find anything here on stackoverflow我在stackoverflow上找不到任何东西

thank you谢谢你

Use GroupBy.transform for divide first values by counts:使用GroupBy.transform将第一个值除以计数:

g = df.groupby(['TaskId','UserId'])['Hours']
df['Hours'] = g.transform('first').div(g.transform('size'))
print (df)

   TaskId  UserId  Hours
0  123456  123456   4.75
1  123456  123456   4.75
2  123456  123456   4.75
3  123456  123456   4.75
4  654321  654321  10.00

暂无
暂无

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

相关问题 Pandas,如何将一行中的值与同一列中的所有其他行进行比较,并将其作为新列中的新行值添加? - Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column? 从 Pandas DataFrame 中选择一列中具有相同值但另一列中具有不同值的行 - Select rows from a Pandas DataFrame with same values in one column but different value in the other column 从 pandas dataframe 中同一字段的所有其他行中减去一行字段中的值 - Subtract the value in a field in one row from all other rows of the same field in pandas dataframe 由于来自不同行的文本值组合在其他 pandas 列中具有相同值,因此创建新的 pandas 行 - Create new pandas row as a result of combination of text values from different rows which has same value in other pandas column Pandas:如何使用其他 dataframe 的列值从 dataframe 返回具有相同行值的行? - Pandas: How to return the row from dataframe having same row values by using column value of other dataframe? 根据其他行(熊猫)更新一行的列 - Update one row's column based on other rows, Pandas 根据 pandas 中的其他列值合并具有相同列值的行 - Merge row with a same column value based on other column values in pandas Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column select 行来自 pandas dataframe 在另一列不同的列中具有相同值并找到平均值并使其成为字典 - select rows from pandas dataframe with same values in one column different on the other &find the average&make it a dictionary 根据Pandas中的特定条件将值从一列复制到同一行中的其他列 - Copy values from one column to other columns in same row based on specific criteria in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM