0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

特定のcolumn 以外を一括でコピーする

Last updated at Posted at 2020-12-16

##list(df_b.columns[~df_b.columns.isin(["val4","val5"]) ]) でval4,val5以外の全ての列を選択出来ます。
#テストデータ

import pandas as pd

a={"idx" :[10,20,30],
   "val1":[11,12,13]}
b={"idx" :[10,20,30],
   "val2":[21,22,23],
   "val3":[31,32,33],
   "val4":[41,42,43],
   "val5":[51,52,53],
   }

df_a=pd.DataFrame(a)
df_a.set_index(["idx"],inplace=True)

df_b=pd.DataFrame(b)
df_b.set_index(["idx"],inplace=True)
print(df_a.to_markdown()) 
print(df_b.to_markdown()) 

df_a

idx val1
10 11
20 12
30 13

df_b

idx val2 val3 val4 val5
10 21 31 41 51
20 22 32 42 52
30 23 33 43 53

#コピー(val4、Va15以外)

cols=list(df_b.columns[~df_b.columns.isin(["val4","val5"]) ])
df_a[cols]=df_b[cols]
print(df_a.to_markdown()) 

df_a

idx val1 val2 val3
10 11 21 31
20 12 22 32
30 13 23 33
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?