chicagobeast12
chicagobeast12

Reputation: 695

Replace values in a row with values from a list in pandas?

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.

Upvotes: 1

Views: 1398

Answers (3)

Rodalm
Rodalm

Reputation: 5433

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

Upvotes: 2

fsimonjetz
fsimonjetz

Reputation: 5802

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

Upvotes: 0

cestlarumba
cestlarumba

Reputation: 198

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

Upvotes: 2

Related Questions