0

I am trying to add new Dataframe column by manipulating other cols.

import pandas as pd
import numpy as np
from pandas import DataFrame, read_csv
from pandas import read_csv
import datetime
df = pd.read_csv('PRSA_data_2010.1.1-2014.12.31.csv')
df.head()

enter image description here

When I am trying to manipulate

df['weekday']= np.int(datetime.datetime(df.year, df.month, df.day).weekday())

I am keep getting error cannot convert the series to class 'int'.

Can anyone tell me a reason behind this and how I can fix it?

Thanks in advance!

jamesdean
  • 21
  • 5

1 Answers1

1

Convert columns to datetimes and then to weekdays by Series.dt.weekday:

df['weekday'] = pd.to_datetime(df[['year', 'month', 'day']].dt.weekday

Or convert columns to datetime column in read_csv:

df = pd.read_csv('PRSA_data_2010.1.1-2014.12.31.csv',
                  date_parser=lambda y,m,d: y + '-' + m + '-' + d, 
                  parse_dates={'datetimes':['year','month','day']})

df['weekday'] = df['datetimes'].dt.weekday
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252