DickyBrown
DickyBrown

Reputation: 93

Finding the Length of a Pandas Dataframe Within a Function

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.

Upvotes: 2

Views: 259

Answers (1)

Andy L.
Andy L.

Reputation: 25239

Just clone a new df and assign 0 to it

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

Upvotes: 5

Related Questions