PageSourceSearch

https://duan602728596.github.io/_next/static/chunks/pages/article/…/common-api-of-pandas-4e6883a405a8f988.js

js duan602728596.github.io collected 2026-10-03 09:05:44 UTC 7,330 bytes, 1 lines download raw bytes

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.