実現したいこと
1.PySimpleGUIで入力フォームを作成。
2.入力フォームに設置してあるComboBox及びInputTextの内容をExcelの対応するところに転記したい。
(例)
【入力フォーム】
日付[2022/4/2](InputText)
名前 [ABC](ComboBox)
【Excel】
日付 名前
20224/2 ABC
※入力フォームはタブがあり入力画面と入力を確認できるログ画面がある。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "ファイル名", line 92, in <module> new_record=pd.DataFrame(values,index=[0]) File "ファイル名", line 636, in __init__ mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager) File "ファイル名", line 502, in dict_to_mgr return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy) File "ファイル名", line 125, in arrays_to_mgr arrays = _homogenize(arrays, index, dtype) File "ファイル名", line 628, in _homogenize com.require_length_match(val, index) File "ファイル名", line 557, in require_length_match raise ValueError( ValueError: Length of values (0) does not match length of index (1)
該当のソースコード
import PySimpleGUI as sg import pandas as pd tab1=[[sg.CalendarButton('日付選択', month_names=[ "{:>2d}月".format(m) for m in range(1, 13) ], format='%Y/%m/%d', locale='ja_JP', key='-button_calendar-', target='日付'),sg.Text('日付:'),sg.I(key='日付', size=(20,1)), sg.Text('名前:'), sg.Combo(com1,key='名前',size=(15,5))], [sg.Button('登録',key='Submit'),sg.Button('クリア',key='clear'),sg.Button('閉じる',key='Exit')]] df1=pd.read_excel('test.xlsx') df1=df1.sort_values(by='日付',ascending=False) list=df1.to_numpy().tolist() header = df1.columns.to_list() L = [[sg.Table(list, headings=header, auto_size_columns=False, vertical_scroll_only=False)]] tab2=[[sg.Text('名前'), sg.Combo(com1,key='名前1',size=(15,5)),sg.B('実行',key='Submit1')], [sg.Table(list, headings=header, auto_size_columns=False, vertical_scroll_only=False)], [sg.Button('更新',key='F5')]] layout = [[sg.TabGroup([[sg.Tab('入力画面',tab1),sg.Tab('ログ',tab2)]],tab_location="topleft")]] window = sg.Window('test', layout, size=(700,400), finalize=True) while True: event,values=window.read() print(event, values) if event=='Submit': wb='test.xlsx' df=pd.read_excel(wb) new_record=pd.DataFrame(values,index=[0]) df=pd.concat([df,new_record],ignore_index=True) df.to_excel(wb,index=False) sg.popup('登録完了') if event == sg.WIN_CLOSED or event == 'Exit': break window.close()
エラー箇所は「new_record=pd.DataFrame(values,index=[0])」です。
試したこと
全てのkey(?項目?)をデータフレーム化するコードになっているので、テーブルのウィジェットが邪魔をしているのではないかと、テーブルなしでやってみたところ、エラーが発生しなかった。
そのため、データフレームにしたい項目だけを選択しようと、
【 new_record=pd.DataFrame([values['日付'],values['名前']],index=['日付','名前']) 】
にしてみたが、
日付 | 名前 | 0 |
---|---|---|
2022/4/2 | ||
ABC |
となる。([Index=]をなくしても同じ結果であった)
[Index=]部を[columns=]にすると
[ File "ファイル名", line 93, in <module> new_record=pd.DataFrame([values['日付'],values['名前']],columns=['日付','名前']) File "ファイル名", line 737, in __init__ mgr = ndarray_to_mgr( File "ファイル名", line 351, in ndarray_to_mgr _check_values_indices_shape_match(values, index, columns) File "ファイル名", line 422, in _check_values_indices_shape_match raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}") ValueError: Shape of passed values is (2, 1), indices imply (2, 2)
と、エラーが発生します。
解決方法について、ご教示いただきますよう、よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/28 09:38 編集