Home > Net >  Iterate a dataframe depending on the value of one column
Iterate a dataframe depending on the value of one column

Time:09-08

I have a (pandas) dataframe like this

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

My goal is to make another dataframe like this

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?

So my question is:

Can I implement the above goal iterating through the f column? and if so, how can this iteration should be written?

CodePudding user response:

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:

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

later you can pd.drop the unwanted columns

CodePudding user response:

You can use .replace() with a dictionary of the 2 values and what they should translate to:

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

Full 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:

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

Output:

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