jason
jason

Reputation: 4449

python pandas wildcard? Replace all values in df with a constant

I have a df and want to make a new_df of the same size but with all 1s. Something to the spirit of: new_df=df.replace("*","1"). I think this is faster than creating a new df from scratch, because i would need to get the dimensions, fill it with 1s, and copy all the headers over. Unless I'm wrong about that.

Upvotes: 0

Views: 787

Answers (2)

Alexander
Alexander

Reputation: 109626

df_new = pd.DataFrame(np.ones(df.shape), columns=df.columns)

import numpy as np
import pandas as pd

d = [
    [1,1,1,1,1],
    [2,2,2,2,2],
    [3,3,3,3,3],
    [4,4,4,4,4],
    [5,5,5,5,5],
]

cols = ["A","B","C","D","E"]

%timeit df1 = pd.DataFrame(np.ones(df.shape), columns=df.columns)
10000 loops, best of 3: 94.6 µs per loop

%timeit df2 = df.copy(); df2.loc[:, :] = 1
1000 loops, best of 3: 245 µs per loop

%timeit df3 = df * 0 + 1
1000 loops, best of 3: 200 µs per loop

Upvotes: 2

WGS
WGS

Reputation: 14179

It's actually pretty easy.

import pandas as pd

d = [
    [1,1,1,1,1],
    [2,2,2,2,2],
    [3,3,3,3,3],
    [4,4,4,4,4],
    [5,5,5,5,5],
]

cols = ["A","B","C","D","E"]

df = pd.DataFrame(d, columns=cols)
print df

print "------------------------"

df.loc[:,:] = 1
print df

Result:

   A  B  C  D  E
0  1  1  1  1  1
1  2  2  2  2  2
2  3  3  3  3  3
3  4  4  4  4  4
4  5  5  5  5  5
------------------------
   A  B  C  D  E
0  1  1  1  1  1
1  1  1  1  1  1
2  1  1  1  1  1
3  1  1  1  1  1
4  1  1  1  1  1

Obviously, df.loc[:,:] means you target all rows across all columns. Just use df2 = df.copy() or something if you want a new dataframe.

Upvotes: 1

Related Questions