6
ファイル数がcsv
のいくつかの値をXML
ファイル(groups.xml
)にグループ化しようとしています。パンダを使用した値のグループ化
Traceback (most recent call last):
File "groups.py", line 69, in <module>
xml_parse()
File "groups.py", line 44, in xml_parse
df['bin'] = pd.cut(frame[1], [float(minval)], include_lowest= mininc)
File "C:\Python27\lib\site-packages\pandas\tools\tile.py", line 113, in cut
include_lowest=include_lowest)
File "C:\Python27\lib\site-packages\pandas\tools\tile.py", line 203, in _bins_to_cuts
include_lowest=include_lowest)
File "C:\Python27\lib\site-packages\pandas\tools\tile.py", line 252, in _format_levels
levels[0] = '[' + levels[0][1:]
IndexError: list index out of range
:
import os, sys
import glob
import pandas as pd
import xml.etree.cElementTree as ET
def xml_parse():
try:
os.chdir("path/to/files")
filename = [file1 for file1 in glob.glob("*.csv")]
filename = [i.split('.', 1)[0] for i in filename]
#filename = '\n'.join(filename)
os.chdir('..')
output = []
doc = ET.parse("groups.xml").getroot()
for root_ele in doc.findall('Groups'):
tag_ele = root_ele.find('GroupID').text
for name in filename:
if name == tag_ele.lower():
for root_ele1 in root_ele.findall('groupname'):
displayname = root_ele1.find('Name').text
minval = root_ele1.find('min').text
mininc = root_ele1.find('minInc').text
maxvalue = root_ele1.find('max')
maxinclusive = root_ele1.find('maxInc')
lists = []
frame = pd.DataFrame()
fname = "path/to/files" + name + ".csv"
df = pd.read_csv(fname, index_col=None, header=None)
lists.append(df)
frame = pd.concat(lists)
if maxvalue is not None:
maxval = maxvalue.text
if maxinclusive is not None:
maxinc = maxinclusive.text
df['bin'] = pd.cut(frame[1], [float(minval),float(maxval)], right= maxinc, include_lowest= mininc)
out = str(pd.concat([df['bin'], frame[1]], axis=1))
out = out.split("\n")[2:]
for a in out:
print a
else:
df['bin'] = pd.cut(frame[1], [float(minval)], include_lowest= mininc)
out = str(pd.concat([df['bin'], frame[1]], axis=1))
out = out.split("\n")[2:]
for a in out:
print a
break
except AttributeError:
pass
電流出力:エラーと
1 NaN 10.18
2 NaN 25.16
3 NaN 44.48
4 NaN 85.24
5 NaN 36.71
6 NaN 77.09
7 NaN 81.88
8 NaN 22.92
9 NaN 44.31
10 NaN 15.79
1 [10, 18] 10.18
2 NaN 25.16
3 NaN 44.48
4 NaN 85.24
5 NaN 36.71
6 NaN 77.09
7 NaN 81.88
8 NaN 22.92
9 NaN 44.31
10 [10, 18] 15.79
1 NaN 10.18
2 [18, 35] 25.16
3 NaN 44.48
4 NaN 85.24
5 NaN 36.71
6 NaN 77.09
7 NaN 81.88
8 [18, 35] 22.92
9 NaN 44.31
10 NaN 15.79
1 NaN 10.18
2 NaN 25.16
3 [35, 50] 44.48
4 NaN 85.24
5 [35, 50] 36.71
6 NaN 77.09
7 NaN 81.88
8 NaN 22.92
9 [35, 50] 44.31
10 NaN 15.79
1 NaN 10.18
2 NaN 25.16
3 NaN 44.48
4 NaN 85.24
5 NaN 36.71
6 NaN 77.09
7 NaN 81.88
8 NaN 22.92
9 NaN 44.31
10 NaN 15.79
1 NaN 10.18
2 NaN 25.16
3 NaN 44.48
4 NaN 85.24
5 NaN 36.71
6 NaN 77.09
7 NaN 81.88
8 NaN 22.92
9 NaN 44.31
10 NaN 15.79
私がある程度機能しますが、私は期待してい何を与えるものではありません。次のコードを持っています期待される出力:
1 [10, 18] 10.18
2 [18, 35] 25.16
3 [35, 50] 44.48
4 [>= 75] 85.24 #however >=75 can be represented
5 [35, 50] 36.71
6 [>= 75] 77.09
7 [>= 75] 81.88
8 [18, 35] 22.92
9 [35, 50] 44.31
10 [10, 18] 15.79
答えをいただき、ありがとうございます。 'ビン'は私には何も返さない。 – pam
私は 'xml'文字列を含んでいませんでした。更新された答えを見てください。 – Stefan
xmlファイルを使用しました。私はそれを正しく読まなかったように見えます。今働いている。ありがとう、トン! – pam