Replace all columns in pandas data frame with one column

I have a pandas dataframe as follows:

    0    1    2    3    4
0   a    b    c    d    e
1   f    g    h    i    j
2   k    l    m    n    o
3   p    q    r    s    t

I want to replace all columns in the data frame with values ​​in column 1, so the result will be

    0    1    2    3    4
0   b    b    b    b    b
1   g    g    g    g    g
2   l    l    l    l    l
3   q    q    q    q    q

How do you do pandas?

+4
source share
3 answers

Consider a data block df

df = pd.DataFrame(np.array(list('abcdefghijklmnopqrst')).reshape(-1, 5))
print(df)

   0  1  2  3  4
0  a  b  c  d  e
1  f  g  h  i  j
2  k  l  m  n  o
3  p  q  r  s  t

Reconstruct

pd.DataFrame(
    np.column_stack([df[1].values] * len(df.columns)),
    df.index, df.columns
)

   0  1  2  3  4
0  b  b  b  b  b
1  g  g  g  g  g
2  l  l  l  l  l
3  q  q  q  q  q
+4
source

One approach will be used [:]to perform assignment on all columns and with the help iloc[:,[1]]to select col-1, keeping the format of the column -

df[:] = df.iloc[:,[1]] # Or df[['1']] if column names are in
               # string sequence from 0 as suggested by @piRSquared

Run Example -

In [15]: df
Out[15]: 
   0  1  2  3  4
0  a  b  c  d  e
1  f  g  h  i  j
2  k  l  m  n  o
3  p  q  r  s  t

In [16]: df[:] = df.iloc[:,[1]]

In [17]: df
Out[17]: 
   0  1  2  3  4
0  b  b  b  b  b
1  g  g  g  g  g
2  l  l  l  l  l
3  q  q  q  q  q

Handling dtype mixed digital frames

, , -1, , 'object' dtype, . , -

df = df.astype('object')
df[:] = df.iloc[:,[1]]

-

In [267]: df = pd.DataFrame(index=range(5), columns=range(10))
     ...: S = pd.Series(np.random.randint(0,9,(18)))
     ...: df[1] = S
     ...: 

In [268]: df
Out[268]: 
     0  1    2    3    4    5    6    7    8    9
0  NaN  3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
1  NaN  3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
2  NaN  1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
3  NaN  0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
4  NaN  6  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

In [269]: df = df.astype('object')
     ...: df[:] = df.iloc[:,[1]]
     ...: 

In [270]: df
Out[270]: 
   0  1  2  3  4  5  6  7  8  9
0  3  3  3  3  3  3  3  3  3  3
1  3  3  3  3  3  3  3  3  3  3
2  1  1  1  1  1  1  1  1  1  1
3  0  0  0  0  0  0  0  0  0  0
4  6  6  6  6  6  6  6  6  6  6
+5

Use np.tilefor repetition

In [1207]: pd.DataFrame(np.tile(df[1].values[:, np.newaxis], len(df.columns)))
Out[1207]:
   0  1  2  3  4
0  b  b  b  b  b
1  g  g  g  g  g
2  l  l  l  l  l
3  q  q  q  q  q

More details

In [1208]: df
Out[1208]:
   0  1  2  3  4
0  a  b  c  d  e
1  f  g  h  i  j
2  k  l  m  n  o
3  p  q  r  s  t
+1
source

Source: https://habr.com/ru/post/1677925/


All Articles