質問編集履歴

1

コードの修正

2022/03/06 11:26

投稿

tomoysh
tomoysh

スコア42

test CHANGED
File without changes
test CHANGED
@@ -23,5 +23,86 @@
23
23
  データフレームからデータaとデータbを参照し、同じならデータcにデータbのデータを入れて、新しいカラムを作りたいと考えています。
24
24
  上記のエラーにより該当のコードでは実行できませんでした。解決方法を教えていただけないでしょうか?
25
25
 
26
+ 追記
26
27
 
28
+ 掲載したコードが誤りでした。正しくは以下のコードでした。
29
+ ```python
30
+ all_df.loc[all_df['データa'] == all_df['データb'], 'データc'] = all_df['データb']
27
31
 
32
+ ```
33
+ 以下エラーの全文になります。
34
+ ValueError Traceback (most recent call last)
35
+ /var/folders/71/9_gw2z712yx4ccsvb21s9kh80000gn/T/ipykernel_28537/4172916488.py in <module>
36
+ ----> 1 all_df.loc[all_df['データa'] == all_df['データb'], 'データc'] = all_df['データb']
37
+
38
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
39
+ 721
40
+ 722 iloc = self if self.name == "iloc" else self.obj.iloc
41
+ --> 723 iloc._setitem_with_indexer(indexer, value, self.name)
42
+ 724
43
+ 725 def _validate_key(self, key, axis: int):
44
+
45
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value, name)
46
+ 1728 if take_split_path:
47
+ 1729 # We have to operate column-wise
48
+ -> 1730 self._setitem_with_indexer_split_path(indexer, value, name)
49
+ 1731 else:
50
+ 1732 self._setitem_single_block(indexer, value, name)
51
+
52
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/indexing.py in _setitem_with_indexer_split_path(self, indexer, value, name)
53
+ 1749 from pandas import Series
54
+ 1750
55
+ -> 1751 value = self._align_series(indexer, Series(value))
56
+ 1752
57
+ 1753 # Ensure we have something we can iterate over
58
+
59
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/indexing.py in _align_series(self, indexer, ser, multiindex_indexer)
60
+ 2118 return ser._values.copy()
61
+ 2119
62
+ -> 2120 return ser.reindex(new_ix)._values
63
+ 2121
64
+ 2122 # 2 dims
65
+
66
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/series.py in reindex(self, index, **kwargs)
67
+ 4578 )
68
+ 4579 def reindex(self, index=None, **kwargs):
69
+ -> 4580 return super().reindex(index=index, **kwargs)
70
+ 4581
71
+ 4582 @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
72
+
73
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/generic.py in reindex(self, *args, **kwargs)
74
+ 4817 # perform the reindex on the axes
75
+ 4818 return self._reindex_axes(
76
+ -> 4819 axes, level, limit, tolerance, method, fill_value, copy
77
+ 4820 ).__finalize__(self, method="reindex")
78
+ 4821
79
+
80
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/generic.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
81
+ 4841 fill_value=fill_value,
82
+ 4842 copy=copy,
83
+ -> 4843 allow_dups=False,
84
+ 4844 )
85
+ 4845
86
+
87
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
88
+ 4887 fill_value=fill_value,
89
+ 4888 allow_dups=allow_dups,
90
+ -> 4889 copy=copy,
91
+ 4890 )
92
+ 4891 # If we've made a copy once, no need to make another one
93
+
94
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/internals/managers.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy, consolidate, only_slice)
95
+ 668 # some axes don't allow reindexing with dups
96
+ 669 if not allow_dups:
97
+ --> 670 self.axes[axis]._validate_can_reindex(indexer)
98
+ 671
99
+ 672 if axis >= self.ndim:
100
+
101
+ /opt/anaconda3/envs/machine/lib/python3.7/site-packages/pandas/core/indexes/base.py in _validate_can_reindex(self, indexer)
102
+ 3783 # trying to reindex on an axis with duplicates
103
+ 3784 if not self._index_as_unique and len(indexer):
104
+ -> 3785 raise ValueError("cannot reindex from a duplicate axis")
105
+ 3786
106
+ 3787 def reindex(
107
+
108
+ ValueError: cannot reindex from a duplicate axis