简体   繁体   English

根据一列的值迭代 dataframe

[英]Iterate a dataframe depending on the value of one column

I have a (pandas) dataframe like this我有一个像这样的(熊猫)dataframe

f  v1 v2
0  1   2
0  2   4
0  0   4
....
0  5    1
1  2   3
1  3   4
...
1  5  7

Let's say this has 100 elements, and half of them are f=0 and half f=1假设这有 100 个元素,其中一半是 f=0,一半是 f=1

My goal is to make another dataframe like this我的目标是像这样制作另一个 dataframe

f  temperature
0  hot
0  hot
0  hot
....
0  hot
1  cold
1  cold
...
1  cold

So I am thinking that it should be a case of iterating through the f column?所以我认为这应该是遍历f 列的情况?

So my question is:所以我的问题是:

Can I implement the above goal iterating through the f column?我可以通过 f 列迭代实现上述目标吗? and if so, how can this iteration should be written?如果是这样,应该如何编写这个迭代?

You can add a 'temperature' columns like this:您可以像这样添加“温度”列:

import numpy as np
df['temperature'] = np.where(df['f'] == 0, 'hot', 'cold')

alternatively, you can map :或者,您可以map

df['tempartature'] = df['f'].map({0: 'cold', 1: 'hot'})

later you can pd.drop the unwanted columns稍后您可以 pd.drop 不需要的列

You can use .replace() with a dictionary of the 2 values and what they should translate to:您可以将.replace()与包含 2 个值的字典以及它们应转换为的内容一起使用:

df['temperature'] = df['f'].replace({0:'hot', 1:'cold'})

Full dataframe:完整的 dataframe:

   f  v1  v2 temperature
0  0   1   2          hot
1  0   2   4          hot
2  0   0   4          hot
3  0   5   1          hot
4  1   2   3         cold
5  1   3   4         cold
6  1   5   7         cold

If you then only want those 2 columns, you can do:如果您只想要这 2 列,则可以执行以下操作:

df = df[['f', 'temperature']]

Output: Output:

   f temperature
0  0         hot
1  0         hot
2  0         hot
3  0         hot
4  1        cold
5  1        cold
6  1        cold

暂无
暂无

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

相关问题 使用每个键的多个值迭代 json 以匹配模式:根据其他列的值设置数据框列值(更快的 Python 解决方案) - Iterate json with multiple values per key for matching pattern: Set dataframe column value depending on value of other column (Faster Python Solution) 汇总按一列分组并取决于其他列值的数据框列中的所有值 - Sum all values in dataframe column grouped by one column and depending on other column value 如何根据相邻列的值从 dataframe 中的一列中读取值? - How to read values from one column in dataframe depending on the value from adjacent column? Python - 如何根据另一列中的值更改 pandas dataframe 的一列中的值组? - Python - How to change groups of values in one column of pandas dataframe depending on a value in another column? Dataframe 迭代一列中的项目并将其存储到数组中 - Dataframe Iterate items in one column and store it into array 根据列将一个数据帧映射到另一个数据帧 - Map one dataframe to another depending on column 根据值是否为 null 创建 pandas dataframe 列 - Create a pandas dataframe column depending if a value is null or not 遍历DataFrame并使用null值更新列 - Iterate through a DataFrame and update column with value if it is null 直方图的颜色条取决于 dataframe 中的列值 - Colouring bars of histogram depending on column value in dataframe 如何根据列值在 Dataframe 中应用 iloc - How to apply iloc in a Dataframe depending on a column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM