2
0

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.

pandasでCSVファイルへ項目名を挿入後SORTして出力する

Posted at

概要

csvcontrol_out_and_sort.py:処理をおこなうpythonファイル
infile.csv:項目行を持たないCSVファイル
outfile.csv:pandasにより項目行を挿入・出力したCSVファイル
sortedoutfile.csv:pandasにより項目列KeyNumbersでソート後出力したCSVファイル
スクリーンショット 2021-06-27 22.43.24.jpg

infileの内容

infile.csv
5555555555,Karen,Walker,19801001,2,1234567,TestCity111-23
3333333333,Anny,Whight,19850401,2,1234567,TestCity111-23
2222222222,Abel,Knolles,19910525,1,1234567,TestCity111-23
1111111111,Tommy,Marland,19860601,1,1234567,TestCity111-23
4444444444,Greg,Abbot,19880801,1,1234567,TestCity111-23

pythonソース

csvcontrol_out_and_sort.py
# 同階層にある項目行を持たないCSVファイルをread
import csv
# pandasで項目名指定を行うSORT機能を利用する
import pandas as pd

# 作業用リストを初期化
work_list01=[]

# インプットファイルオープン
infile = open('infile.csv', 'r')

print(">>---------------------------")
print(">>infile.csvを読み込んでいます...")
# inのCSVファイルを一旦全件workに読み込む
for row in csv.reader(infile):
    work_list01.append(row)

# 項目名を準備
columns01 = ['KeyNumbers','FirstName','FamilyName',
             'BirthDay','sex','PostNumbers','Address']

# pandasのDataFrameに項目名の指定とともに渡す
df01 = pd.DataFrame(data=work_list01,columns=columns01,dtype='object')
# 読み込んだ直後の状態を表示
print(">>---------------------------")
print(">>【infile:pandas】")
print(df01)

print(">>pandasによる項目行追加済のcsv出力を実行します....")
df01.to_csv("outfile.csv",index=False)

print(">>---------------------------")
print(">>pandasによるsortを実行します....")
# ソート処理
df01.sort_values(['KeyNumbers'])

# 読み込んだ直後の状態を表示
print(">>【infile:pandas.sort_values】")
print(df01.sort_values(['KeyNumbers']))

print(">>pandasによるsort済のcsv出力を実行します....")
df01.sort_values(['KeyNumbers']).to_csv("sortedoutfile.csv",index=False)

# ファイルクローズ
infile.close()
print(">>処理を終了します.")
print(">>---------------------------")

処理結果

ターミナル実行

>>---------------------------
>>infile.csvを読み込んでいます...
>>---------------------------
>>【infile:pandas】
   KeyNumbers FirstName FamilyName  BirthDay sex PostNumbers         Address
0  5555555555     Karen     Walker  19801001   2     1234567  TestCity111-23
1  3333333333     Anny     Whight  19850401   2     1234567  TestCity111-23
2  2222222222      Abel    Knolles  19910525   1     1234567  TestCity111-23
3  1111111111     Tommy    Marland  19860601   1     1234567  TestCity111-23
4  4444444444      Greg      Abbot  19880801   1     1234567  TestCity111-23
>>pandasによる項目行追加済のcsv出力を実行します....
>>---------------------------
>>pandasによるsortを実行します....
>>【infile:pandas.sort_values】
   KeyNumbers FirstName FamilyName  BirthDay sex PostNumbers         Address
3  1111111111     Tommy    Marland  19860601   1     1234567  TestCity111-23
2  2222222222      Abel    Knolles  19910525   1     1234567  TestCity111-23
1  3333333333     Anny     Whight  19850401   2     1234567  TestCity111-23
4  4444444444      Greg      Abbot  19880801   1     1234567  TestCity111-23
0  5555555555     Karen     Walker  19801001   2     1234567  TestCity111-23
>>pandasによるsort済のcsv出力を実行します....
>>処理を終了します.
>>---------------------------

出力ファイル

outfile.csv
KeyNumbers,FirstName,FamilyName,BirthDay,sex,PostNumbers,Address
5555555555,Karen,Walker,19801001,2,1234567,TestCity111-23
3333333333,Anny,Whight,19850401,2,1234567,TestCity111-23
2222222222,Abel,Knolles,19910525,1,1234567,TestCity111-23
1111111111,Tommy,Marland,19860601,1,1234567,TestCity111-23
4444444444,Greg,Abbot,19880801,1,1234567,TestCity111-23

→項目行が設定されています。

sortedoutfile.csv
KeyNumbers,FirstName,FamilyName,BirthDay,sex,PostNumbers,Address
1111111111,Tommy,Marland,19860601,1,1234567,TestCity111-23
2222222222,Abel,Knolles,19910525,1,1234567,TestCity111-23
3333333333,Anny,Whight,19850401,2,1234567,TestCity111-23
4444444444,Greg,Abbot,19880801,1,1234567,TestCity111-23
5555555555,Karen,Walker,19801001,2,1234567,TestCity111-23

→KeyNumbersで昇順にSORTされています。

おわり

できました.
今度は突合かな...

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?