前提・実現したいこと
Test2.csvのうちID列の2行目以降全てに"S_"を付与した上で,Test1.csvのID列に従いTest2.csvのID・Nubmer列を並べ直したいと考えています.お知恵をお貸し頂けますと幸いです.宜しくお願い致します.
尚,こちらは本データ(1000行以上)を簡略化したものです.
該当のソースコード
python
1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3import pandas as pd 4 5input_path1 = "Test.csv" 6input_path2 = "Test2.csv" 7output_path = "output.csv" 8df1 = pd.read_csv(filepath_or_buffer=input_path1, encoding="utf-8") 9df2 = pd.read_csv(filepath_or_buffer=input_path2, encoding="utf-8") 10df1 = df1.set_index('ID') 11df2 = df2.set_index('ID') 12 13for column_name, item in df2.iteritems(): 14 item = "S_" + item 15 df2 = df1.reindex_like(df2) 16 17with open(output_path, mode='w') as f: 18 f.write()
エラーメッセージ
Traceback (most recent call last): File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 968, in na_op result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/computation/expressions.py", line 221, in evaluate return _evaluate(op, op_str, a, b, **eval_kwargs) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/computation/expressions.py", line 70, in _evaluate_standard return op(a, b) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/roperator.py", line 9, in radd return right + left numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32') During handling of the above exception, another exception occurred: Traceback (most recent call last): File "narabekae.py", line 14, in <module> item = "S_" + item File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 1048, in wrapper result = na_op(lvalues, rvalues) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 970, in na_op result = masked_arith_op(x, y, op) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 464, in masked_arith_op result[mask] = op(xrav[mask], y) File "/Users/macuser/downloads/yes/lib/python3.7/site-packages/pandas/core/ops/roperator.py", line 9, in radd return right + left numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
理想の状態 (output.csv)
output.csv
1ID,Number 2S_AA,2.13 3S_BB,2.21 4S_CC,2.09 5S_DD,2.38 6S_EE,2.52
使用ファイル1 (Test1.csv)
Test1.csv
1ID,Number 2S_AA,2.1 3S_BB,2.2 4S_CC,2.3 5S_DD,2.4 6S_EE,2.5
使用ファイル2 (Test2.csv)
Test2.csv
1ID,Number 2CC,2.09 3EE,2.52 4AA,2.13 5DD,2.38 6BB,2.21
補足情報(FW/ツールのバージョンなど)
macOS10.15.4 Python3.7.3 Atom
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/09 04:04
2020/11/09 04:16