質問編集履歴
1
「コードの挿入」を使っていなかったので、直しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
|
2
|
+
PWM_VitroDB+PSP_b=√Nという名前のディレクトリに約400個のテキストファイルがあります。
|
3
3
|
それらすべてをデータフレームとして読み込むところまではできています。
|
4
4
|
これらのデータフレームそれぞれに、何らかの形で名前を付けたいです。
|
5
5
|
色々調べましたが、うまくいきません。初心者ですがよろしくお願いします。
|
6
6
|
|
7
7
|
### 該当のソースコード
|
8
8
|
|
9
|
+
```
|
9
10
|
from glob import glob
|
10
11
|
import pandas as pd
|
11
12
|
|
12
|
-
files=glob("
|
13
|
+
files=glob("PWM_VitroDB+PSP_b=√N/*.txt")
|
13
14
|
|
14
15
|
for file in files:
|
15
16
|
a=files.index(file)
|
16
17
|
df[a]=pd.read_table(file,header=None)
|
18
|
+
```
|
17
19
|
|
18
20
|
### コメント
|
19
21
|
|
@@ -23,4 +25,114 @@
|
|
23
25
|
それともディクショナリ?などを使って番号とファイル名を1:1対応させ、番号でアクセスするのが普通でしょうか。
|
24
26
|
|
25
27
|
説明が分かりにくかったらすみません。
|
26
|
-
よろしくお願いします。
|
28
|
+
よろしくお願いします。
|
29
|
+
|
30
|
+
### Error
|
31
|
+
(長くてすみません…)
|
32
|
+
|
33
|
+
```
|
34
|
+
KeyError Traceback (most recent call last)
|
35
|
+
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
|
36
|
+
2896 try:
|
37
|
+
-> 2897 return self._engine.get_loc(key)
|
38
|
+
2898 except KeyError:
|
39
|
+
|
40
|
+
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
|
41
|
+
|
42
|
+
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
|
43
|
+
|
44
|
+
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
|
45
|
+
|
46
|
+
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
|
47
|
+
|
48
|
+
KeyError: 14
|
49
|
+
|
50
|
+
During handling of the above exception, another exception occurred:
|
51
|
+
|
52
|
+
KeyError Traceback (most recent call last)
|
53
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value)
|
54
|
+
1068 try:
|
55
|
+
-> 1069 loc = self.items.get_loc(item)
|
56
|
+
1070 except KeyError:
|
57
|
+
|
58
|
+
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
|
59
|
+
2898 except KeyError:
|
60
|
+
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
|
61
|
+
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
|
62
|
+
|
63
|
+
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
|
64
|
+
|
65
|
+
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
|
66
|
+
|
67
|
+
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
|
68
|
+
|
69
|
+
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
|
70
|
+
|
71
|
+
KeyError: 14
|
72
|
+
|
73
|
+
During handling of the above exception, another exception occurred:
|
74
|
+
|
75
|
+
ValueError Traceback (most recent call last)
|
76
|
+
<ipython-input-73-82b18bb0ee6f> in <module>
|
77
|
+
6 for file in files:
|
78
|
+
7 a=files.index(file)
|
79
|
+
----> 8 df[a]=pd.read_table(file,header=None)
|
80
|
+
|
81
|
+
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
|
82
|
+
3470 else:
|
83
|
+
3471 # set column
|
84
|
+
-> 3472 self._set_item(key, value)
|
85
|
+
3473
|
86
|
+
3474 def _setitem_slice(self, key, value):
|
87
|
+
|
88
|
+
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
|
89
|
+
3548 self._ensure_valid_index(value)
|
90
|
+
3549 value = self._sanitize_column(key, value)
|
91
|
+
-> 3550 NDFrame._set_item(self, key, value)
|
92
|
+
3551
|
93
|
+
3552 # check if we are modifying a copy
|
94
|
+
|
95
|
+
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _set_item(self, key, value)
|
96
|
+
3379
|
97
|
+
3380 def _set_item(self, key, value):
|
98
|
+
-> 3381 self._data.set(key, value)
|
99
|
+
3382 self._clear_item_cache()
|
100
|
+
3383
|
101
|
+
|
102
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value)
|
103
|
+
1070 except KeyError:
|
104
|
+
1071 # This item wasn't present, just insert at end
|
105
|
+
-> 1072 self.insert(len(self.items), item, value)
|
106
|
+
1073 return
|
107
|
+
1074
|
108
|
+
|
109
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in insert(self, loc, item, value, allow_duplicates)
|
110
|
+
1179 new_axis = self.items.insert(loc, item)
|
111
|
+
1180
|
112
|
+
-> 1181 block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
|
113
|
+
1182
|
114
|
+
1183 for blkno, count in _fast_count_smallints(self._blknos[loc:]):
|
115
|
+
|
116
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath)
|
117
|
+
3265 values = DatetimeArray._simple_new(values, dtype=dtype)
|
118
|
+
3266
|
119
|
+
-> 3267 return klass(values, ndim=ndim, placement=placement)
|
120
|
+
3268
|
121
|
+
3269
|
122
|
+
|
123
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
|
124
|
+
2773 values = np.array(values, dtype=object)
|
125
|
+
2774
|
126
|
+
-> 2775 super().__init__(values, ndim=ndim, placement=placement)
|
127
|
+
2776
|
128
|
+
2777 @property
|
129
|
+
|
130
|
+
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
|
131
|
+
126 raise ValueError(
|
132
|
+
127 "Wrong number of items passed {val}, placement implies "
|
133
|
+
--> 128 "{mgr}".format(val=len(self.values), mgr=len(self.mgr_locs))
|
134
|
+
129 )
|
135
|
+
130
|
136
|
+
|
137
|
+
ValueError: Wrong number of items passed 14, placement implies 1
|
138
|
+
```
|