कहें, मैंने डेटाफ्रेम दिया है जिसमें अधिकांश कॉलम स्पष्ट डेटा हैं।डेटाफ्रेम को सब्सक्राइब करने के लिए किसी निर्देश का उपयोग कैसे करें?
> data.head()
age risk sex smoking
0 28 no male no
1 58 no female no
2 27 no male yes
3 26 no male no
4 29 yes female yes
और मैं इस डेटा को उन स्पष्ट चर के लिए कुंजी-मूल्य जोड़े के एक नियम द्वारा सबसेट करना चाहता हूं।
tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'}
इसलिए, मैं निम्नलिखित सबसेट प्राप्त करना चाहता हूं।
data[ (data.risk == 'no') & (data.smoking == 'yes') & (data.sex == 'female')]
मुझे क्या करना चाहते हैं:
data[tmp]
ऐसा करने का सबसे अजगर/पांडा तरीका क्या है?
मिनिमल उदाहरण:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
x = Series(random.randint(0,2,50), dtype='category')
x.cat.categories = ['no', 'yes']
y = Series(random.randint(0,2,50), dtype='category')
y.cat.categories = ['no', 'yes']
z = Series(random.randint(0,2,50), dtype='category')
z.cat.categories = ['male', 'female']
a = Series(random.randint(20,60,50), dtype='category')
data = DataFrame({'risk':x, 'smoking':y, 'sex':z, 'age':a})
tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'}
और यदि आप इंडेक्स को संरक्षित करना चाहते हैं? – lanery
@Ianery 'विलय (डेटाफ्रेम (tmp, index = [0]), data.reset_index())। Set_index ('index') 'काम करता है, हालांकि आदर्श नहीं है। – Psidom