-1

I would like to rename the Weekday to day_of_week in dataframe below

Weekday    Value
2          10
2          5
8          50
7          20
4          11

using

df = df.rename(columns = {'Weekday' : 'day_of_week'})

I then notice that df has been converted to pandas.core.series.Series from DataFrame, which is not what I meant, because I would like to replace all 8 with 1 in the column in a DataFrame, and I'm not sure how to do it with a series.

My question is:

1) what are the different ways of renaming a column in a pandas dataframe and the differences(regarding data type transformation)?

2) what is the best way to solve the problem in my case and how to do it?

Thanks


Update: Sorry the problem lies with replacing values 8 with 1 in column day_of_week after renaming.

New problem: I used

df['day_of_week'].replace(['8'], '1') 
df['day_of_week'].describe()

and it showed that the values had not changed. What's the correct way of doing this?

nilsinelabore
  • 4,143
  • 17
  • 65
  • 122

1 Answers1

0

Check the type and index of your dataframe at all times. The rename method applied to a dataframe always returns a dataframe.

df.index
type(df)

You could use:

#df=df.reset_index() # if Weekday is the index
df['Weekday']=df['Weekday'].mask(df['Weekday']>7,df['Weekday']%7)
df=df.rename(columns = {'Weekday' : 'day_of_week'})
print(df)

Output

   day_of_week  Value
0            2     10
1            2      5
2            1     50
3            7     20
4            4     11

Dataframe.rename is what we use to rename the columns although you could use:

df.columns=[the names of your columns]
ansev
  • 30,322
  • 5
  • 17
  • 31
  • Thanks for the answer. I used `df['day_of_week'].replace(['8'], '1')` to replace values `8` with `1` but `df['day_of_week'].describe()` showed that the values didn't change. Could you please tell me what's wrong? – nilsinelabore Dec 04 '19 at 23:52
  • Could it be that your values ​​are not str? – ansev Dec 04 '19 at 23:59
  • add "inplace" option and make types in replace the same as in df: df['day_of_week'].replace(8, 1, inplace=True) – Stepan Dec 04 '19 at 23:59
  • keep in mind that if it has a value like 9 or 10 or 23 you will have to include them in replace while the solution that I have budgeted does it always – ansev Dec 05 '19 at 00:00
  • `df['day_of_week']=df['day_of_week'].replace(['8'], '1')` or `df['day_of_week']=df['day_of_week'].replace([8], 1)` – ansev Dec 05 '19 at 00:01
  • Thank you. It worked:) May I ask what the difference is between `replace` and `inplace`? – nilsinelabore Dec 05 '19 at 00:06
  • inplace is an argument that is used so that the dataframe on which the methods apply is updated with the dataframe resulting from the methods applied. While replace is a method to replace dataframe values – ansev Dec 05 '19 at 00:08
  • normally instead of using for example `df.replace(8,1, inplace = True)` we use `df = df.replace(8,1)` – ansev Dec 05 '19 at 00:09
  • https://stackoverflow.com/questions/43893457/understanding-inplace-true – ansev Dec 05 '19 at 00:11
  • Is _`df['Weekday'].mask(df['Weekday']>7,df['Weekday']%7)`_ better than just `df['Weekday'].subtract(1).mod(7).add(1)`? – AMC Dec 05 '19 at 05:39