I am trying to find the common elements from all the columns of a dataframe which looks like the following:
0 1 2
0 PPRFP.1T22AD0010 PPRFP.1T22AD0010 PPRFP.1T22AD0010
1 TRRFF.1T22AD0033 TRRFF.1T22AD0033 TRRFF.1T22AD0033
2 TRRFF.1T22AD0004 TRRFF.1T22AD0004 TRRFF.1T22AD0004
3 TRRFF.1T22AD0038 TRRFF.1T22AD0038 TRRFF.1T22AD0038
4 TRRFF.1T22AD0025 TRRFF.1T22AD0025 TRRFF.1T22AD0025
.. ... ... ...
91 PPRFP.1T22AD0028 PPRFP.1T22AD0028 PPRFP.1T22AD0028
92 HTRFF.1T22AD0004 HTRFF.1T22AD0004 HTRFF.1T22AD0004
93 TRRFF.1T22AD0034 TRRFF.1T22AD0034 TRRFF.1T22AD0034
94 PPRFP.1T22AD0040 PPRFP.1T22AD0040 PPRFP.1T22AD0040
95 TRRFF.1T22AD0007 TRRFF.1T22AD0007 TRRFF.1T22AD0007
like the following:
comon_dict = set.intersection(*df_last1.agg(set))
earlier it was working fine but when all the columns have same elements it shows the following error:
TypeError: 'set' type is unordered
Full error stack:
Traceback (most recent call last):
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\inventory\views.py", line 325, in get
dic = set.intersection(*df_last1.agg(set))
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\frame.py", line 8550, in aggregate
result = op.agg()
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\apply.py", line 715, in agg
result = self.obj.apply(self.orig_f, axis, args=self.args, **self.kwargs)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\frame.py", line 8740, in apply
return op.apply()
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\apply.py", line 688, in apply
return self.apply_standard()
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\apply.py", line 815, in apply_standard
return self.wrap_results(results, res_index)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\apply.py", line 841, in wrap_results
return self.wrap_results_for_axis(results, res_index)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\apply.py", line 909, in wrap_results_for_axis
result = self.obj._constructor(data=results)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\frame.py", line 614, in __init__
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\internals\construction.py", line 464, in dict_to_mgr
return arrays_to_mgr(
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\internals\construction.py", line 124, in arrays_to_mgr
arrays = _homogenize(arrays, index, dtype)
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\internals\construction.py", line 589, in _homogenize
val = sanitize_array(
File "C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\venv\lib\site-packages\pandas\core\construction.py", line 559, in sanitize_array
raise TypeError(f"'{type(data).__name__}' type is unordered")
TypeError: 'set' type is unordered
How do I solve this?
CodePudding user response:
Is this what you're looking for?
set(df.values.ravel())
Output:
{'PPRFP.1T22AD0028', 'TRRFF.1T22AD0025',
'PPRFP.1T22AD0010', 'TRRFF.1T22AD0007',
'HTRFF.1T22AD0004', 'TRRFF.1T22AD0034',
'TRRFF.1T22AD0033', 'TRRFF.1T22AD0004',
'TRRFF.1T22AD0038', 'PPRFP.1T22AD0040'}