Finding the Length of a Pandas Dataframe Within a Function

Viewed 155

The objective of the code below is to create another identical pandas dataframe, where all values are replaced with zero.

input numpy as np
import pandas as pd
#Given preexisting dataframe
len(df) #Returns 1502

def zeroCreator(data):
    zeroFrame = pd.DataFrame(np.zeros(len(data),1))
    return zeroFrame
print(zeroCreator(df)) #Returns a TypeError: data type not understood

How do I work around this TypeError?

Edit: Thank you for all your clarifications, it appears that I hadn't entered the dataframe parameters correctly into np.zeros (missing a pair of parentheses), although a simpler solution does exist.

1 Answers

Just clone a new df and assign 0 to it

zero_df = df.copy()
zero_df[:] = 0
Related