pandasでCSVファイルへ項目名を挿入後SORTして出力する
概要
csvcontrol_out_and_sort.py:処理をおこなうpythonファイル
infile.csv:項目行を持たないCSVファイル
outfile.csv:pandasにより項目行を挿入・出力したCSVファイル
sortedoutfile.csv:pandasにより項目列KeyNumbersでソート後出力したCSVファイル
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
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
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
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されています。
おわり
できました.
今度は突合かな...
Author And Source
この問題について(pandasでCSVファイルへ項目名を挿入後SORTして出力する), 我々は、より多くの情報をここで見つけました https://qiita.com/sejiijes/items/11d66754839e7dd68198著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .