3
2

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 5 years have passed since last update.

1列のcsvファイルを列方向に連結する

Posted at

1列のcsvファイルを列方向に連結します。
読み込んだままの1次元の形式では、concatenateさせると行方向の連結になるため、2次元に形式を変換してから処理をしてみました。
行数はそろえておく必要があります。

qiita.rb

import os
import numpy as np

# n行1列のcsv同士を連結させる

data3 = np.genfromtxt("./csv/data_03.csv", delimiter=",", dtype='float')
data4 = np.genfromtxt("./csv/data_04.csv", delimiter=",", dtype='float')

print(data3)
print(data4)

# 今回読み込ませた csvは、10行1列のデータ。1次元で読み込まれる。
# data3
# [ 11.  21.  31.  41.  51.  61.  71.  81.  91. 101.]
# data4
# [ 101.  201.  301.  401.  501.  601.  701.  801.  901. 1001.]

data3_plus_data4_0 = np.concatenate([data3, data4], axis=0)

print(data3_plus_data4_0)

# axis=0 この場合、1次元のままなので、2n行1列になる。
# data3_plus_data4_0
# [  11.   21.   31.   41.   51.   61.   71.   81.   91.  101.  101.  201.
#  301.  401.  501.  601.  701.  801.  901. 1001.]

reshape_data3 = np.reshape(data3, (10, 1))  # 10行1列の2次元に変換
reshape_data4 = np.reshape(data4, (10, 1))  # 10行1列の2次元に変換

print(reshape_data3)
print(reshape_data4)

# 2次元に変換される。
# [[ 11.]
# [ 21.]
# [ 31.]
#  ...
# [ 81.]
# [ 91.]
# [101.]]

data3_plus_data4_1 = np.concatenate([reshape_data3, reshape_data4], axis=1)

print(data3_plus_data4_1)

# n行2列で連結
# [[  11.  101.]
# [  21.  201.]
# [  31.  301.]
#  ...
# [  81.  801.]
# [  91.  901.]
# [ 101. 1001.]]

# 結果をcsvファイルに出力
np.savetxt("./out/data3_plus_data4.csv", data3_plus_data4_1, delimiter=",")




3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?