1

I have the following df:

import pandas as pd

d = {
    'Group': ['Month', 'Sport'],
    'col1': [1, 'A'],
    'col2': [4, 'B'],
    'col3': [9, 'C']
}
df = pd.DataFrame(d)

I would like to convert all of the values in row index[0] excluding 'Month' to actual months. I've tried the following:

import datetime as dt

m_lst = []
for i in df.iloc[0]:
    if type(i) != str:
        x = dt.date(1900, i, 1).strftime('%B')
        m_lst.append(x)

df.iloc[0][1:] = m_lst  #(doesn't work)

So the for loop creates a list of months that correlate to the value in the dataframe. I just can't seem to figure out how to replace the original values with the values from the list. If there's an easier way of doing this, that would be great as well.

2
  • Seems to work fo me if I understand correctly your intended output. Edit your question perhaps to show what you are getting/what you want to get. Commented Apr 8, 2022 at 21:42
  • Just wow... So the data here is example data as I can't share my real data, however everything from the table structure to the solution is the same... not sure what the problem is but thanks for pointing this out Commented Apr 8, 2022 at 21:46

3 Answers 3

2

You can convert those values to datetime using pandas.to_datetime and then use the month_name property

import pandas as pd

d = {
    'Group': ['Month', 'Sport'],
    'col1': [1, 'A'],
    'col2': [4, 'B'],
    'col3': [9, 'C']
}
df = pd.DataFrame(d)

df.iloc[0, 1:] = pd.to_datetime(df.iloc[0, 1:], format='%m').dt.month_name()

Output:

>>> df

   Group     col1   col2       col3
0  Month  January  April  September
1  Sport        A      B          C
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming your month numbers are always in the same position, row 0, I'd use iloc and apply lambda like this:

import datetime as dt
import pandas as pd


def month_number_to_str(m: int):
    return dt.datetime.strptime(str(m), '%m').strftime('%B')


d = {
    'Group': ['Month', 'Sport'],
    'col1': [1, 'A'],
    'col2': [4, 'B'],
    'col3': [9, 'C']
}
df = pd.DataFrame(d)
df.iloc[0, 1:] = df.iloc[0, 1:].apply(lambda x: month_number_to_str(x))
print(df)

Output:

   Group     col1   col2       col3
0  Month  January  April  September
1  Sport        A      B          C

Comments

0

Another way is to use Series.map. It can translate values for you, e.g., based on a dictionary like this (where you get it is up to you):

months = {1: 'January',
          2: 'February',
          3: 'March',
          4: 'April',
          5: 'May',
          6: 'June',
          7: 'July',
          8: 'August',
          9: 'September',
          10: 'October',
          11: 'November',
          12: 'December'}

Then it's just a matter of selecting the right part of df and mapping the values:

>>> df.iloc[0, 1:] = df.iloc[0, 1:].map(month_dict)
>>> df
   Group     col1   col2       col3
0  Month  January  April  September
1  Sport        A      B          C

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.