1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[9337],{39003:function(n,e,a){(window.__NEXT_P=window.__NEXT_P||[]).push(["/article/python/common-api-of-pandas",function(){return a(92349)}])},92349:function(n,e,a){"use strict";a.r(e),a.d(e,{default:function(){return u}});var d=a(85893),s=a(67294),r=a(9008),l=a.n(r),c=a(26219),i=a(72253),o=a(14932),p=a(11151);function t(n){var e=(0,i._)({code:"code",h1:"h1",li:"li",p:"p",pre:"pre",ul:"ul"},(0,p.a)(),n.components);return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(e.h1,{children:"pandasç常ç¨API"}),"\n",(0,d.jsx)(e.p,{children:"pandasçå¸¸ç¨æä½æ¹æ³ï¼ä¾¿äºå¿«éæ¥æ¾ã"}),"\n",(0,d.jsxs)(e.ul,{children:["\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"å è½½æ°æ®"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df = pd.DataFrame(data)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"éå½åå"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df.rename(columns={\n 'a': 'new_a',\n 'b': 'new_b',\n 'c': 'new_c',\n}, inplace = True)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"mergeæ°æ®"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"merged_df = pd.DataFrame([{ 'id: '1', 'd': 'd_value' }])\ndf = pd.merge(df, merged_df, on=['id'], how='left')\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"å并两个DataFrame"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df1 = pd.DataFrame(data1)\ndf = pd.concat([df, df1], ignore_index=True)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"éæ°æåå"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df = df[['id', 'a', 'b', 'c', 'd']]\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æ·»å è¡"}),"\n"]}),"\n"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:" new_row = pd.Series({\n 'id': '32',\n 'a': 'a_value',\n 'b': 'b_value',\n 'c': 'c_value',\n 'd': 'd_value',\n })\n df.loc[len(df)] = new_row\n"})}),"\n",(0,d.jsxs)(e.ul,{children:["\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æ·»å å"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df['e'] = ['e1_value', 'e2_value', 'e3_value']\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"ç¨applyæ·»å å"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"def apply_callback(row):\n \"\"\"为æ¯ä¸ªåæ·»å æ°å±æ§\"\"\"\n row['f'] = row['e'] + row['c']\n return row\n\ndf = df.apply(apply_callback, axis=1)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æ ¹æ®æ¡ä»¶è®¡ç®/æ·»å å"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"import pandas as pd\nimport numpy as np\n\nraw_data = [\n { 'a': 1, 'b': 3, 'c': 4 },\n { 'a': 2, 'b': None, 'c': 5 },\n { 'a': 3, 'b': 8, 'c': None },\n { 'a': 4, 'b': None, 'c': 7 },\n]\ndf = pd.DataFrame(raw_data)\n\n\ndef add(a, b, c):\n return np.where(a.isna() | b.isna() | c.isna(), 'ç®ä¸åºæ¥', a + b + c)\n\n\ndf['sum'] = add(df.a, df.b, df.c)\nprint(df)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æ¿æ¢å¼"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df.replace([np.inf, -np.inf], np.nan, inplace=True) # å é¤inf\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"å é¤å"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"df = df.drop(columns=['e', 'c'])\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"å 餿宿¡ä»¶çè¡"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"# è¿ä¸ªä½ç½®ä¸è½ç¨is True, andè¿ç§æ¹å¼\ncon = df['a'] == 13\n# æ\ncon = df.a == 13\n# æ\ncon = df.a.isna() | df.b.empty\ndf = df.drop(df[con].index, axis=0)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æ´æ°æ°æ®"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"con = df['id'] == '14'\ndf.loc[con, 'a'] = 95\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æå®çindexæå
¥æ°æ®"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"insert_value_index = 2\ninsert_value_df = pd.DataFrame(value, index=[insert_value_index])\ndf = pd.c
1oncat([df.loc[:insert_value_index],\n insert_value_df,\n df.loc[insert_value_index + 1:]],\n ignore_index=True)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"使ç¨ç¬¦å·"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"import pandas as pd\n\nraw_data = [\n { 'a': 1, 'b': 1 },\n { 'a': 1, 'b': 0 },\n { 'a': 0 },\n]\n\ndf = pd.DataFrame(raw_data)\ndf['OR'] = (df.a < 1) | (df.b < 1)\ndf['AND'] = (df.a < 1) & (df.b < 1)\ndf['NOT'] = ~(df.a < 1)\nprint(df)\n"})}),"\n"]}),"\n",(0,d.jsxs)(e.li,{children:["\n",(0,d.jsx)(e.p,{children:"æåº"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"# é« -> ä½\nsort1_df = df.sort_values(['a'], ascending=False)\n# ä½ -> é«\nsort2_df = df.sort_values(['b'])\n"})}),"\n"]}),"\n"]})]})}function h(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=(0,i._)({},(0,p.a)(),n.components).wrapper;return e?(0,d.jsx)(e,(0,o._)((0,i._)({},n),{children:(0,d.jsx)(t,(0,i._)({},n))})):t(n)}var x=function(n){return(0,d.jsx)(c.Z,{children:(0,d.jsx)(h,{})})};function u(n){return(0,d.jsxs)(s.Fragment,{children:[(0,d.jsxs)(l(),{children:[(0,d.jsx)("title",{children:"pandasç常ç¨API"}),(0,d.jsx)("meta",{name:"keywords",content:"pandas"}),(0,d.jsx)("meta",{name:"description",content:"pandasç常ç¨API"})]}),(0,d.jsx)(x,{})]})}},94914:function(n,e,a){"use strict";var d=a(85893),s=a(73935),r=a(39332),l=a(39738),c=a(52856),i=a(70612),o=a(30448),p=a(76467),t=a(1294),h=a.n(t),x=l.Z.BackTop;e.Z=function(n){var e=(0,r.useRouter)();return[(0,d.jsxs)("div",{className:h().main,children:[n.goBack?(0,d.jsx)("div",{className:"mb-[16px] text-right",children:(0,d.jsxs)(c.ZP,{onClick:function(n){e.push("/")},children:[(0,d.jsx)(o.Z,{className:"mr-[6px]"}),"è¿å"]})}):null,null==n?void 0:n.children]},"main"),(null==n?void 0:n.goToTop)?(0,d.jsx)(p.default,{children:"object"==typeof document?(0,s.createPortal)((0,d.jsx)(x,{type:"primary",icon:(0,d.jsx)(i.Z,{}),title:"è¿åé¡¶é¨",tabIndex:0,role:"button","aria-label":"è¿åé¡¶é¨"}),document.body):null},"backTop"):null]}},26219:function(n,e,a){"use strict";var d=a(85893),s=a(66252),r=a(94914);e.Z=function(n){return(0,d.jsx)(r.Z,{goToTop:!0,goBack:!0,children:(0,d.jsx)(s.Z,{children:n.children})})}},1294:function(n){n.exports={main:"main_main__IZXSv",goToTop:"main_goToTop__Py8c8"}}},function(n){n.O(0,[2856,8375,2888,9774,179],function(){return n(n.s=39003)}),_N_E=n.O()}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.