एक डेटाफ़्रेम df
को निम्नानुसार दिया गया है:
id room area check
0 1 A-102 world NaN
1 2 NaN 24 room name is not valid
2 3 B309 NaN NaN
3 4 C·102 25 room name is not valid
4 5 E_1089 hello room name is not valid
5 6 27 NaN NaN
6 7 27 NaN NaN
मैं यह जांचना चाहता हूं कि क्या area
कॉलम वैध डेटा प्रारूप है, यदि यह numbers
या NaN
s है, तो मान लें कि यह वैध डेटा है, या तो, check
कॉलम को check
के साथ अपडेट करें। area is not a number
.
मैंने df.loc[df.area.str.contains('^\d+$', na = True), 'check'] = 'area is not a number'
के साथ प्रयास किया, लेकिन मुझे वह नहीं मिला जिसकी मुझे आवश्यकता थी।
मैं इस तरह एक अपेक्षित परिणाम कैसे प्राप्त कर सकता हूं:
id room area check
0 1 A-102 world area is not a number
1 2 NaN 24 room name is not valid
2 3 B309 NaN NaN
3 4 C·102 25 room name is not valid
4 5 E_1089 hello room name is not valid; area is not a number
5 6 27 NaN NaN
6 7 27 NaN NaN
आपकी मदद के लिए अग्रिम धन्यवाद।
1 उत्तर
आप करीब हैं, केवल ~
द्वारा मुखौटा उल्टा करें:
df.loc[~df.area.str.contains('^\d+$', na = True), 'check'] = 'area is not a number'
print(df)
id room area check
0 1 A-102 world area is not a number
1 2 NaN 24 NaN
2 3 B309 NaN NaN
3 4 C·102 25 NaN
4 5 E_1089 hello area is not a number
5 6 27 NaN NaN
6 7 27 NaN NaN
या Series.where
:
df['check'] = df['check'].where(df.area.str.contains('^\d+$', na = True),
'area is not a number')
संपादित करें:
m1 = df.room.str.contains('([^a-zA-Z\d\-])', na = True)
m2 = df.area.str.contains('^\d+$', na = True)
v1 = 'room name is not valid'
v2 = 'area is not a number'
df['check'] = np.where(m1 & ~m2, v1 + ', ' + v2,
np.where(m1, v1,
np.where(~m2, v2, None)))
print(df)
id room area check
0 1 A-102 world area is not a number
1 2 NaN 24 room name is not valid
2 3 B309 NaN None
3 4 C 102 25 room name is not valid
4 5 E_1089 hello room name is not valid, area is not a number
5 6 27 NaN None
6 7 27 NaN None
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
python-3.x
पायथन प्रोग्रामिंग के बारे में प्रश्नों के लिए जो भाषा के संस्करण 3+ के लिए विशिष्ट हैं। सभी पायथन सवालों पर अधिक जेनेरिक [अजगर] टैग का उपयोग करें, और केवल यह जोड़ें यदि आपका प्रश्न संस्करण-विशिष्ट है। पायथन 2 प्रश्नों के लिए [अजगर -2] टैग का उपयोग करें।