2017-05-25 17 views
0

私はcsvファイルに入力を書き込む単純なデータ入力フォームを持っています。どこかのプロセスでファイルに余分な列が追加されていることを除いて、すべてが正常に機能しているようですが、ユーザーの入力段階にあるようです。ユーザー入力フォームに不明な列が追加されました

import pandas as pd 

#adds all spreadsheets into one list 
Batteries= ["MAT0001.csv","MAT0002.csv", "MAT0003.csv", "MAT0004.csv", 
"MAT0005.csv", "MAT0006.csv", "MAT0007.csv", "MAT0008.csv"] 


#User selects battery to log 
choice = (int(input("Which battery? (1-8):"))) 
def choosebattery(c): 
    done = False 
    while not done: 
     if(c in range(1,9)): 
      return Batteries[c] 
      done = True 
     else: 
      print('Sorry, selection must be between 1-8') 
cfile = choosebattery(choice) 

cbat = pd.read_csv(cfile) 


#Collect Cycle input 
print ("Enter Current Cycle") 
response = None 
while response not in {"Y", "N", "y", "n"}: 
    response = input("Please enter Y or N: ") 
    cy = response 

#Charger input  
print ("Enter Current Charger") 
response = None 
while response not in {"SC-G", "QS", "Bosca", "off", "other"}: 
    response = input("Please enter one: 'SC-G', 'QS', 'Bosca', 'off', 'other'") 
    if response == "other": 
     explain = input("Please explain") 
     ch = response + ":" + explain 
    else: 
     ch = response 

#Location 
print ("Enter Current Location") 
response = None 
while response not in {"Rack 1", "Rack 2", "Rack 3", "Rack 4", "EV001", "EV002", "EV003", "EV004", "Floor", "other"}: 
    response = input("Please enter one: 'Rack 1 - 4', 'EV001 - 004', 'Floor' or 'other'") 
    if response == "other": 
     explain = input("Please explain") 
     lo = response + ":" + explain 
    else: 
     lo = response  

#Voltage   
done = False 
while not done: 
    choice = (float(input("Enter Current Voltage:"))) 
    modchoice = choice * 10 
    if(modchoice in range(500,700)): 
     vo = choice 
     done = True 
    else: 
     print('Sorry, selection must be between 50 and 70') 


#add inputs to current battery dataframe 
log = pd.DataFrame([[cy,ch,lo,vo]],columns=["Cycle", "Charger", "Location", "Voltage"]) 
clog = pd.concat([cbat,log], axis=0) 




clog.to_csv(cfile, index = False) 
pd.read_csv(cfile) 

そして、私が受け取る: "名前" 欄から来ている

Out[18]: 
    Charger Cycle Location Unnamed: 0 Voltage 
0  off  n  Floor   NaN  50.0 

ここでは、コードのですか?

答えて

0

あなたのcsvには「名前のない」列があります。その理由は、入力csvファイルの行がカンマ(つまり区切り文字)で終わる可能性が高いため、pandasはそれを追加の(名前のない)列として解釈します。その場合は、あなたの行がセパレータで終わるかどうかを確認してください。中へ

Column1,Column2,Column3, 
val_11, val12, val12, 
... 

:たとえば、あなたのファイルはカンマで区切られている場合は別の方法として

Column1,Column2,Column3 
val_11, val12, val12 
... 

this answerのように明示的にインデックス列を指定してみてください。私は混乱の一部がパンダに由来すると信じていますconcatreordering your columns

+0

最初の考えは有望に見えますが、どうすればそれを改善できるかは考えられません。私はすでにindex = Falseを渡しているので、index以外のものでなければなりません。私は失っている、私はこれのための簡単な回避策を見つけることができない、私は別のメソッドやデータ構造を使用している必要がありますか? – Josmolio

+0

@Josmolioこれは、各行の末尾の区切り記号を削除するだけの場合です。質問を例で更新しました – vmg

関連する問題