{"id":511,"date":"2019-07-06T23:16:00","date_gmt":"2019-07-06T15:16:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=511"},"modified":"2023-11-03T23:18:39","modified_gmt":"2023-11-03T15:18:39","slug":"python-pandas-access","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/511","title":{"rendered":"Python pandas access"},"content":{"rendered":"\n<p>\u63a7\u5236dataframe<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5916\u90e8\u8cc7\u6599\u8b80\u53d6\u8207\u5beb\u5165<\/strong><\/h2>\n\n\n\n<p><strong>\u8b80\u6a94<\/strong><br>pd.read_csv()<br>ex:<br>#cat data.csv<br>date,value<br>2016,100<br>2015,90<br>2014,50<br>>>>data=pd.read_csv(&#8216;data.csv&#8217;)<br>>>>data.date<br>0 2016<br>1 2015<br>2 2014<br>ex:<br>pd.read_csv(&#8216;ch09\/stock_px.csv&#8217;, parse_dates=True, index_col=0)<br>ex:<br>temp_df = pd.read_csv(&#8216;file.csv&#8217;, header=None, names=[&#8216;FileID&#8217;, &#8216;CustomerID&#8217;, &#8216;QueryTS&#8217;, &#8216;ProductID&#8217;])<\/p>\n\n\n\n<p><br><strong>\u5132\u5b58<\/strong><br>pd.to_csv()<br>ex:<br>pd.to_csv(&#8220;analysis_result.csv&#8221;)<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8cc7\u6599\u8f49\u63db<\/strong><\/h2>\n\n\n\n<p>\u8f49\u63db<br><strong>pandas to series<\/strong><br>.series()<br>ex:<br>ts = pd.Series(df[&#8216;Value&#8217;].values, index=df[&#8216;Date&#8217;])<\/p>\n\n\n\n<p><strong>pandas to numpy<\/strong><br>.values<br>ex:<br>npdata=df.values<\/p>\n\n\n\n<p><strong>pandas to list<\/strong><br>.to_list()<br>ex:<br>listdata=df.to_list()<\/p>\n\n\n\n<p><strong>dict to pandas<\/strong><br>ex:<br>groups = [&#8220;Web&#8221;, &#8220;Dev&#8221;, &#8220;Cloud&#8221;, &#8220;Data&#8221;, &#8220;Security&#8221;, &#8220;group_p&#8221;]<br>groupvalue = [5, 9, 1, 4, 6, 7]<br>data_dict = {&#8220;groups&#8221;: groups,&#8221;value&#8221;: groupvalue}<br>data_df = pd.DataFrame(data_dict)<\/p>\n\n\n\n<p><strong>list to pandas<\/strong><br>ex:<br>listfieldlen = [59, 9, 19, 14, 6, 77]<br>df_fieldcount = pd.DataFrame()<br>df_fieldcount[&#8216;fieldcount&#8217;]=listfieldlen<br>ex:<br>listfieldlen = [59, 9, 19, 14, 6, 77]<br>df_listfieldlen = pd.DataFrame(listfieldlen)<\/p>\n\n\n\n<p>refer<br>https:\/\/ithelp.ithome.com.tw\/articles\/10185922<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8cc7\u6599\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p><br><strong>\u8a2d\u5b9a\u6b04\u4f4d\u578b\u5225<\/strong><br>.astype( &lt; type&gt; )<br>ex:<br>df[&#8220;Department&#8221;] =df[&#8220;Department&#8221;].astype(&#8220;category&#8221;)<\/p>\n\n\n\n<p><strong>\u522a\u9664\u6307\u5b9a\u7684\u6b04\u4f4d<\/strong><br>.drop( &lt; field name &gt; )<br>ex:<br>data=data.drop(data.columns[0],axis=1)<br>data=data.drop(&#8220;column name&#8221;,axis=1)<br>ex:<br>#\u522a\u9664\u6307\u5b9a\u6b04\u4f4d<br>&gt; datatrain.drop([&#8216;agenttime&#8217;],axis=1,inplace=True)<\/p>\n\n\n\n<p><br><strong>\u5206\u5272\u6b04\u4f4d\u8cc7\u6599<\/strong><br>.str.split( &lt; char&gt; )<br>ex:<br>data3[&#8216;URL&#8217;].str.split(&#8216;?&#8217;, expand=True)<\/p>\n\n\n\n<p><strong>\u5408\u4f75\u6b04\u4f4d<\/strong><br>#\u900f\u904e\u5408\u4f75\u6b04\u4f4d\u5efa\u7acb\u65b0\u6b04\u4f4d<br>ex:<br>&gt; datatrain[\u2018ip&#8217;] = datatrain[\u2018ipv4&#8242;] + datatrain[\u2018ipv6&#8242;]<\/p>\n\n\n\n<p><strong>\u4ee5\u7d2f\u7a4d\u65b9\u5f0f\u5408\u4f75\u8cc7\u6599<\/strong><br>.append()<br>ex:<br>datatrain=train1.append(train2, ignore_index=True)<\/p>\n\n\n\n<p><br><strong>\u6c34\u5e73\u5408\u4f75<br><\/strong>.concat<br>ex:<br>datanew = pd.concat([data1,data2],axis=1, join_axes=[data1.index])<\/p>\n\n\n\n<p>&#8230;&nbsp;<\/p>\n\n\n\n<p><strong>\u5c07\u7a7a\u503c(NaN)\u88dc0<\/strong><br>.fillna()<br>ex:<br>df.fillna(0)<br>ex:<br># \u5c07\u7a7a\u503c\u88dc\u6eff<br>&gt; datatrain[\u2018account&#8217;].fillna(&#8220;null&#8221;, inplace=True)<\/p>\n\n\n\n<p><strong>\u53d6\u4ee3<\/strong><br>.replace()<br>\u6b64\u65b9\u6cd5\u9700\u5148\u900f\u904edictionary\u5c07\u5b57\u4e32\u8b8a\u6210\u4ee3\u865f<br>ex:<br>&gt; dict_content={&#8216;Tcp&#8217;:0,&#8217;TCP&#8217;:0,&#8217;udp&#8217;:1,&#8217;UDP&#8217;:1}<br>&gt; datatrain[\u2018protocol&#8217;].replace(dict_content,regex=True)<\/p>\n\n\n\n<p><strong>\u5c07\u8cc7\u6599\u524d\u5f8c\u7a7a\u767d\u522a\u9664<\/strong><br>df.str.strip()<\/p>\n\n\n\n<p><strong>\u8f49\u63db\u6642\u9593\u683c\u5f0f<\/strong><br>to_datetime( &lt; time&gt;, format=&#8221; &lt; time format&gt;&#8221;)<br>ex:<br>pd.to_datetime(&#8220;6 Mar 2019 00:54:40&#8221;,format=&#8217;%d %b %Y %H:%M:%S&#8217;)<br>&gt;2019-03-06 00:54:40<\/p>\n\n\n\n<p>&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a7\u5236dataframe &#8230;&#8230;&#038;#82 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-develop"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=511"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}