Home > Mobile >  Syntax logic between Pandas and numpy
Syntax logic between Pandas and numpy

Time:10-04

I have a syntax question between numpy and Pandas, and probably more Python modules. To be effective in numpy can do this :

b = np.ones((5,5))
b[1:-1, 1:-1] = 0
print(b)

But, when i want to do something on a Dataframe with Pandas i have use a variable else it doesn't work, like this :

df2 = pd.concat(out, ignore_index=True, axis=1)
df2 = df2.fillna(method='ffill')

Expect df.to_csv for example. I don't understand why Python's modules doesn't use similar syntax. Am i missing something ?

CodePudding user response:

It does work, it's just not the default for pandas. You have to do it like this:

df2.fillna(method='ffill', inplace=True)

Probably this is not the default because it wouldn't nesting operations like this:

df2 = pd.concat(out, ignore_index=True, axis=1).fillna(method='ffill').replace(...).apply(...)

Pandas actually uses numpy arrays. you can access them like this

df.values

or this

df["column"].values
  • Related