मैं पैंडस क्रॉसस्टैब्स के लिए कॉलम ऑर्डर करने का तरीका नहीं ढूंढ रहा हूं। मुझे विशेष रूप से मेरे कॉलम ऑर्डर करने की आवश्यकता है जो दिनांकित मानों के आधार पर स्वरूपित दिनांक (mmm yy) हैं और महीने के 3-अक्षर नाम (मिमीएम) पर क्रमबद्ध रूप से क्रमबद्ध नहीं हैं।पांडस क्रॉसस्टैब: स्वरूपित दिनांक के रूप में नामित कॉलम का क्रम बदलें (mmm yy)
अजगर 3.3
पांडा 0.12.0
f_dtflt
एक पांडा dataframe है:
यहाँ मेरी कोड के विवरण हैं।
pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, f_dtflt.COLLECTION_DATE.apply(lambda x: x.strftime("%b %y")), margins=True)
उत्पादन होता है:
COLLECTION_DATE Apr 13 Aug 13 Dec 12 Feb 13 Jan 13 Jul 13 Jun 13
EW_REGIONCOLLSITE
EAST 1964 2092 2280 2272 2757 2113 1902
WEST 2579 2011 1003 2351 2216 1506 1823
All 4543 4103 3283 4623 4973 3619 3725
COLLECTION_DATE Mar 13 May 13 Nov 12 Oct 12 Sep 13 All
EW_REGIONCOLLSITE
EAST 1682 1981 2108 825 975 22951
WEST 2770 3014 407 42 888 20610
All 4452 4995 2515 867 1863 43561
मैं चाहता हूँ कॉलम तिथि आरोही द्वारा आदेश दिया जा करने के लिए
f_dtflt.COLLECTION_DATE
dtype datetime64 [एनएस]
मेरे crosstab बयान है ... 12 अक्टूबर, 12 नवंबर, ... 13 जनवरी, ... सितंबर 13. मुझे पता है कि मैं तिथियों को प्रारूपित कर सकता हूं कि वे yy-mm हैं (उदा। 13-01) लेकिन इन लेबलों का इस्तेमाल एक रिपोर्ट में किया जाएगा और यह एक समझौता है जिसे मैं नहीं करना चाहता हूं।
मैं अजगर और पांडा के लिए नया हूं इसलिए कृपया अपने प्रतिक्रियाओं में किसी भी बिंदु को जोड़कर नौसिखिया की मदद करें! बहुत बहुत धन्यवाद।
विधि का @ एंडी जवाब पहले भाग के जवाब में 1
संपादित करें। चरण 3:
के साथ कोई समस्या है मैंने एंडी के सुझाव को लागू करने का प्रयास किया है और यहां इस प्रयास पर अधिक जानकारी है।
1) मैं यह देखने के लिए निम्न पंक्ति चलाता हूं कि तिथियां कैसी दिखती हैं। निम्न पंक्ति संग्रह तिथि के लिए '2012-10' जैसे मान बनाती है। (प्रिंट द्वारा "सजा"?)
print(pd.DatetimeIndex(f_dtflt['COLLECTION_DATE']).to_period('M'))
2) ऊपर बयान crosstab में प्रवेश किया है, यह क्षेत्र में इस तरह के रूप में 513, 514, आदि (वास्तविक मान अंक के लिए महीने के मूल्यों में परिवर्तन?
col_0 513 514 515 516 517 518 519 520 521 522
EW_REGIONCOLLSITE
EAST 825 2108 2280 2757 2272 1682 1964 1981 1902 2113
WEST 42 407 1003 2216 2351 2770 2579 3014 1823 1506
All 867 2515 3283 4973 4623 4452 4543 4995 3725 3619
col_0 523 524 All
EW_REGIONCOLLSITE
EAST 2092 975 22951
WEST 2011 888 20610
All 4103 1863 43561
3) जब मैं निम्नलिखित कोड चलाने के लिए, यह एक त्रुटि फेंकता है कि 'int' ऑब्जेक्ट कोई विशेषता 'strftime'
table1.columns = table1.columns.map(lambda x: x.strftime("%b %y"))
है:)
table1=pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, pd.DatetimeIndex(f_dtflt['COLLECTION_DATE']).to_period('M'), margins=True)
यहाँ उत्पादन होता है
मैंने इस के साथ बहुत कुछ खेला और यहां मेरे कुछ नोट्स हैं:
# This runs and creates an array of strings: '513' etc.
pd.to_datetime(table1.columns.map(str), unit='M')
# The last entry in table1.columns is "All" and needs to be removed. Hence [:-1] slice.
# This also runs but seems to give years in 1630's.
pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M')
# This does not run because it says object is immutable
table1.columns[:-1]=pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M')
# This also runs but the output is weird. It seems to give an array of both dates and -1
table1.columns.reindex(pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M'))
# Does not run: DatetimeIndex() must be called with a collection of some kind, '513' was passed
table1.columns = table1.columns.map(lambda x: pd.DatetimeIndex(str(x)).strftime("%b %y"))
# Does not run: DatetimeIndex object is not callable
table1.rename(columns=pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M'))
4) यह crosstab में स्तंभों लेबलिंग के लिए काम करता है:
table1.columns.name = 'COLLECTION_DATE'
विधि 2
@Andy एक दूसरे सुझाव दिया और मैं इसके साथ चारों ओर खेला और यह करने के लिए नहीं मिल सका काम। इस मुद्दे का एक बड़ा हिस्सा अजगर, पांडा, और numpy के साथ परिचितता की कमी है। मैंने खुद के लिए नोट्स बनाये क्योंकि मैंने इसे हल करने का प्रयास किया था। यहाँ मेरी नोट नहीं हैं:
# Working with a new concept
# This creates row titles of 12 10, 12 11, etc.
table1=pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, f_dtflt.COLLECTION_DATE.apply(lambda x: x.strftime("%y %m")), margins=True)
# This throws an error that yb is not defined
table1.columns.map(lambda yb: "%s %s" % (y, b) for y, b in yb.split())
# Tried to simplify and see what happens. Runs and creates an array of lists such as [['12, '10'], ['12', '11']...]
table1.columns.map(lambda x: x.split())
# Trying a different approach. This creates a numpy array of datetimes.
tempholder=table1.columns[:-1].map(lambda x: datetime.datetime(year=int(x[0:2]), month=int(x[3:]), day=1))
# Noted that f_dtflt['COLLECTION_DATE'] was a dtype of datetime64[ns] but tempholder was dtype object. So had issue.
# Convert to datetime64
# Get error: Out of bounds nanosecond timestamp: 12-10-01 00:00:00
tempholder=pd.to_datetime(tempholder)
# Tempholder is an array of datetimes from the datetime module. I used the pandas date function above.
# Need to change that and use python datetime module function.
# Does not work: 'numpy.ndarray' object has no attribute 'apply'...
# this is a pandas function which does not work on a numpy array.
tempholder.apply(lambda x: x.strftime('%b %y'))
# This works for numpy array but I can't tell what it contains.
# print(tempholder) gives <map object at 0x0000000026C04F28>
# tempholder gives Out[169]: <builtins.map at 0x26c04f28>
tempholder=map(lambda x: x.strftime('%b %y'), tempholder)
क्यों आप अपने 'COLLECTION_DATE' स्तंभ पहले छांटे नहीं और उसके बाद' crosstab' कारगर साबित होंगे? – EdChum
@ एडीचम, हाँ, ने कोशिश की। क्रॉसस्टैब सॉर्ट ऑर्डर को मजबूर करता है।क्रॉसस्टैब के लिए कोई तर्क नहीं मिला जो मुझे सॉर्ट ऑर्डर को नियंत्रित करने या इसे बंद करने देता है। –