How can I simplify adding columns with certain values to my dataframe?

FKenny

I have a big dataframe (more than 900000 rows) and want to add some columns depending on the first column (Timestamp with date and time). My code works, but I guess it's far too complicated and slow. I'm a beginner so help would be appreciated! Thanks!

df['seconds_midnight'] = 0
df['weekday'] = 0
df['month'] = 0

def date_to_new_columns(date_var, i):
    sec_after_midnight = dt.timedelta(hours=date_var.hour, minutes=date_var.minute, seconds=date_var.second).total_seconds()
    weekday = dt.date.isoweekday(date_var)
    month1 = date_var.month
    df.iloc[i, 24] = sec_after_midnight
    df.iloc[i, 25] = weekday
    df.iloc[i, 26] = month1
    return


for i in range(0, 903308):
    date_to_new_columns(df.timestamp.iloc[i], i)
ArkF

So the reason this is slow is the for loop processing each row individually. One thing that makes pandas so nice is that you can quickly process whole columns/dataframes in one operation.

So create all the rows for each new column at the same time:

def date_to_new_columns(df):
    df['sec_after_midnight'] = (df.timestamp - df.timestamp.dt.normalize()).dt.seconds
    df['weekday'] = df.timestamp.dt.day_name
    df['month1'] = df.timestamp.dt.month
    return

Note that the dt.day_name method is called dt.weekday_name prior to pandas version 0.23.0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i reformat row values of a certain column of my dataframe

How would I take a row-wise average values for certain columns, while retaining other in my dataframe?

How to I simplify code that combines information and adds up values for certain columns?

How can I remove all but certain words from a column with alphanumerical values in my dataframe?

How can I subtract values of each column of my dataframe from all other columns?

How can I keep all the rows in my dataframe that have the same values in all columns?

How can I access certain columns in a DataFrame based on a list?

How can I drop rows with certain values from a dataframe?

How can I cap my volume for above certain values?

How to reshape the dataframe by adding and subtracting values in columns

How can I normalize the data in a range of columns in my pandas dataframe

How can I insert actual values in dataframe as columns in R?

How can I insert values from a list to DataFrame columns?

How can I group my values into columns by category in Power BI?

How can I form my dataframe so that each row with same id becomes one row and the values change into columns

How can I turn my ts forecast into a dataframe with date values?

How can I count comma-separated values in my Dataframe?

How can I simplify this?

How I can simplify this?

How can all values of certain included or excluded columns of a DataFrame be impuded based on a condition?

How can I simplify my if-else statement in java?

How can I use function or loop to simplify my program?

How can I simplify my pandas script using a loop?

How can I simplify my nested for-loops with ReactiveCocoa?

How can I simplify my jQuery code to avoid repeating instructions?

How can i simplify my python quiz? (make it shorter)

How can I simplify my long-winded code?

How can i simplify my current React code?

How can I simplify my "equation" to switch between 3 and 5?