numpy.lib.recfunctions.join_by
यह केवल संरचित सरणियों या recarrays पर काम करता है देखें, तो वहाँ अड़चनों के एक जोड़े हैं।
सबसे पहले आपको संरचित सरणी से कम से कम कुछ हद तक परिचित होना चाहिए। यदि आप नहीं हैं तो here देखें।
import numpy as np
import numpy.lib.recfunctions
# Define the starting arrays as structured arrays with two fields ('key' and 'field')
dtype = [('key', np.int), ('field', np.float)]
x = np.array([(1, 2),
(2, 4),
(3, 6),
(4, np.NaN),
(5, 10)],
dtype=dtype)
y = np.array([(0, -5),
(1, 0),
(2, 5),
(5, 20),
(6, 25)],
dtype=dtype)
# You want an outer join, rather than the default inner join
# (all values are returned, not just ones with a common key)
join = np.lib.recfunctions.join_by('key', x, y, jointype='outer')
# Now we have a structured array with three fields: 'key', 'field1', and 'field2'
# (since 'field' was in both arrays, it renamed x['field'] to 'field1', and
# y['field'] to 'field2')
# This returns a masked array, if you want it filled with
# NaN's, do the following...
join.fill_value = np.NaN
join = join.filled()
# Just displaying it... Keep in mind that as a structured array,
# it has one dimension, where each row contains the 3 fields
for row in join:
print row
यह आउटपुट:
(0, nan, -5.0)
(1, 2.0, 0.0)
(2, 4.0, 5.0)
(3, 6.0, nan)
(4, nan, nan)
(5, 10.0, 20.0)
(6, nan, 25.0)
आशा है कि मदद करता है!
संपादित 1: जोड़ा गया उदाहरण संपादित 2: वास्तव में फ्लोट्स के साथ शामिल नहीं होना चाहिए ... एक int में 'कुंजी' फ़ील्ड बदल दिया गया।
इस अंतर्दृष्टिपूर्ण प्रतिक्रिया के लिए धन्यवाद। मेरी मूर्खता के लिए, क्या संरचना सरणी को ndarray में परिवर्तित करने का एक आसान तरीका है? धन्यवाद। – leon
@leon - यहां एक तरीका है (उदाहरण में "शामिल" सरणी का उपयोग करके ...): join.view (np.float) .reshape ((join.size, 3)) आशा है कि मदद करता है! –
यह वास्तव में काम नहीं करता है क्योंकि पहला कॉलम int के रूप में डाला जाता है। यही कारण है कि मैं पूछ रहा था। – leon