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