{"id":84339,"date":"2026-07-26T11:42:31","date_gmt":"2026-07-26T03:42:31","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/84339.html"},"modified":"2026-07-26T11:42:31","modified_gmt":"2026-07-26T03:42:31","slug":"pandas-dataframe-%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95%e5%85%a8%e6%b1%87%e6%80%bb%ef%bc%88%e5%85%a5%e9%97%a8%e5%ae%9e%e6%93%8d%e5%8d%9a%e6%96%87%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/84339.html","title":{"rendered":"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u4f7f\u7528 pandas \u505a\u6570\u636e\u5206\u6790&#xff0c;\u9664\u4e86 DataFrame \u57fa\u7840\u521b\u5efa\u3001\u7d22\u5f15\u53d6\u503c&#xff0c;\u7edd\u5927\u591a\u6570\u5de5\u4f5c\u90fd\u4f9d\u8d56\u5404\u7c7b\u5185\u7f6e\u65b9\u6cd5&#xff1a;\u67e5\u770b\u5934\u5c3e\u3001\u5217\u7b5b\u9009\u3001\u5e03\u5c14\u8fc7\u6ee4\u3001\u7edf\u8ba1\u8ba1\u7b97\u3001\u53bb\u91cd\u3001\u6392\u5e8f\u3001\u968f\u673a\u62bd\u6837\u7b49\u3002\u672c\u7bc7\u57fa\u4e8e\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u5206\u95e8\u522b\u7c7b\u8bb2\u89e3\u9ad8\u9891\u65b9\u6cd5&#xff0c;\u9002\u914d\u6570\u636e\u5206\u6790\u65e5\u5e38\u4f7f\u7528\u3002<\/p>\n<p>\u63d0\u524d\u56fa\u5b9a\u6f14\u793a\u6570\u636e\u96c6&#xff0c;\u5168\u6587\u6240\u6709\u6848\u4f8b\u57fa\u4e8e\u8fd9\u4efd\u8868\u683c\u8fd0\u884c&#xff1a;<\/p>\n<p>import pandas as pd<\/p>\n<p>df &#061; pd.DataFrame({<br \/>\n    &#034;name&#034;:[&#034;tom&#034;,&#034;tom&#034;,&#034;jack&#034;,&#034;alice&#034;,&#034;bob&#034;,&#034;allen&#034;],<br \/>\n    &#034;age&#034;:[15,15,15,20,26,30],<br \/>\n    &#034;score&#034;:[60.5,60.5,80,30.6,70,83.5]<br \/>\n},<br \/>\nindex&#061;[&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;,&#039;e&#039;,&#039;f&#039;],  # \u81ea\u5b9a\u4e49\u884c\u6807\u7b7e<br \/>\ncolumns&#061;[&#034;name&#034;,&#034;score&#034;,&#034;age&#034;])    # \u81ea\u5b9a\u4e49\u5217\u987a\u5e8f<\/p>\n<h3>\u4e00\u3001\u67e5\u770b\u6570\u636e\u5934\u5c3e&#xff1a;head () \/tail ()<\/h3>\n<p>\u53ea\u770b\u5c40\u90e8\u884c&#xff0c;\u4e0d\u7528\u6253\u5370\u5168\u8868&#xff0c;\u9002\u5408\u5feb\u901f\u9884\u89c8\u6570\u636e\u3002<\/p>\n<li>df.head(n)&#xff1a;\u67e5\u770b\u524d n \u884c&#xff0c;\u4e0d\u4f20\u53c2\u9ed8\u8ba4\u5c55\u793a\u524d 5 \u884c<\/li>\n<li>df.tail(n)&#xff1a;\u67e5\u770b\u6700\u540e n \u884c&#xff0c;\u4e0d\u4f20\u53c2\u9ed8\u8ba4\u5c55\u793a\u6700\u540e 5 \u884c<\/li>\n<p># \u67e5\u770b\u524d3\u884c<br \/>\nprint(&#034;head\u524d3\u884c&#xff1a;\\\\n&#034;,df.head(3))<br \/>\n# \u67e5\u770b\u540e3\u884c<br \/>\nprint(&#034;tail\u540e3\u884c&#xff1a;\\\\n&#034;,df.tail(3))<\/p>\n<p># \u9ed8\u8ba45\u884c<br \/>\nprint(&#034;\u9ed8\u8ba4\u524d5\u884c&#xff1a;&#034;,df.head())<br \/>\nprint(&#034;\u9ed8\u8ba4\u540e5\u884c&#xff1a;&#034;,df.tail())<\/p>\n<p>\u5c0f\u6280\u5de7&#xff1a;\u53ef\u4ee5\u94fe\u5f0f\u642d\u914d\u5217\u7b5b\u9009&#xff0c;\u5148\u9009\u5217\u518d\u53d6\u5934\u5c3e&#xff0c;\u7cbe\u7b80\u6570\u636e&#xff1a; df[[&#039;name&#039;,&#039;score&#039;]].head(3) \u53ea\u770b\u59d3\u540d\u3001\u5206\u6570\u7684\u524d\u4e09\u884c<\/p>\n<h3>\u4e8c\u3001\u5355\u5217 \/ \u591a\u5217\u83b7\u53d6&#xff0c;\u642d\u914d\u5934\u5c3e\u4f7f\u7528<\/h3>\n<h4>1. \u5355\u5217 3 \u79cd\u5199\u6cd5\u533a\u522b<\/h4>\n<p>\u8868\u683c<\/p>\n<table>\n<tr>\u5199\u6cd5\u8fd4\u56de\u7c7b\u578b\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>df[&#039;name&#039;]<\/td>\n<td>Series \u4e00\u7ef4<\/td>\n<td>\u5e38\u7528&#xff0c;\u9002\u5408\u5355\u72ec\u8ba1\u7b97<\/td>\n<\/tr>\n<tr>\n<td>df.name<\/td>\n<td>Series \u4e00\u7ef4<\/td>\n<td>\u5217\u540d\u65e0\u7a7a\u683c \/ \u7279\u6b8a\u5b57\u7b26\u624d\u80fd\u7528<\/td>\n<\/tr>\n<tr>\n<td>df[[&#039;name&#039;]]<\/td>\n<td>DataFrame \u4e8c\u7ef4<\/td>\n<td>\u4fdd\u7559\u8868\u683c\u683c\u5f0f&#xff0c;\u63a8\u8350\u4f5c\u56fe\u3001\u591a\u5217\u7edf\u4e00\u5199\u6cd5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. \u591a\u5217\u83b7\u53d6<\/h4>\n<p>\u591a\u5217\u5fc5\u987b\u7528\u5217\u8868\u5305\u88f9\u5217\u540d&#xff1a;df[[&#039;\u52171&#039;,&#039;\u52172&#039;]]<\/p>\n<p># \u53d6name\u3001score\u4e24\u5217<br \/>\nprint(df[[&#039;name&#039;,&#039;score&#039;]])<br \/>\n# \u4e24\u5217&#043;\u524d3\u884c<br \/>\nprint(df[[&#039;name&#039;,&#039;score&#039;]].head(3))<br \/>\n# \u5355\u5217age&#043;\u6700\u540e3\u884c<br \/>\nprint(df[[&#039;age&#039;]].tail(3))<\/p>\n<h3>\u4e09\u3001\u5e03\u5c14\u7d22\u5f15&#xff1a;\u6309\u6761\u4ef6\u7b5b\u9009\u884c&#xff08;\u6700\u6838\u5fc3\u7b5b\u9009&#xff09;<\/h3>\n<p>\u8bed\u6cd5&#xff1a;df[\u6761\u4ef6\u8868\u8fbe\u5f0f]&#xff0c;\u652f\u6301\u5355\u6761\u4ef6\u3001\u591a\u6761\u4ef6\u7ec4\u5408<\/p>\n<h4>\u89c4\u5219<\/h4>\n<li>\u591a\u6761\u4ef6\u5fc5\u987b\u7528\u62ec\u53f7\u5305\u88f9\u6bcf\u4e2a\u6761\u4ef6<\/li>\n<li>\u4e14&#xff1a;&amp;&#xff1b;\u6216&#xff1a;|&#xff1b;\u4e0d\u80fd\u7528 and\/or<\/li>\n<li>\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7&#xff1a;df.score \u548c df[&#039;score&#039;]<\/li>\n<p># \u5355\u6761\u4ef6&#xff1a;\u5206\u6570\u5927\u4e8e70<br \/>\nprint(df[df.score&gt;70])<br \/>\nprint(df[df[&#039;score&#039;]&gt;70])<\/p>\n<p># \u591a\u6761\u4ef6&#xff1a;\u5206\u6570\u5927\u4e8e70 \u5e76\u4e14 \u5e74\u9f84\u5c0f\u4e8e30<br \/>\nres &#061; df[(df[&#039;score&#039;]&gt;70) &amp; (df[&#039;age&#039;] &lt; 30)]<br \/>\nprint(res)<\/p>\n<h3>\u56db\u3001\u5339\u914d\u5224\u65ad\u3001\u7a7a\u503c\u5224\u65ad&#xff1a;isin () \/isna ()<\/h3>\n<h4>1. isin () \u5224\u65ad\u5355\u5143\u683c\u662f\u5426\u5305\u542b\u6307\u5b9a\u5185\u5bb9<\/h4>\n<p>\u7528\u6765\u7b5b\u9009\u5b57\u6bb5\u5728\u67d0\u4e2a\u540d\u5355\u91cc&#xff0c;\u652f\u6301\u591a\u5b57\u6bb5\u5339\u914d<\/p>\n<p># \u6240\u6709\u5355\u5143\u683c\u662f\u5426\u7b49\u4e8ejack<br \/>\nprint(df.isin([&#039;jack&#039;]))<br \/>\n# \u5339\u914d jack \u6216\u8005\u6570\u5b5720<br \/>\nprint(df.isin([&#039;jack&#039;,20]))<\/p>\n<h4>2. isna () \u5224\u65ad\u7a7a\u503c<\/h4>\n<p>\u6807\u8bb0\u8868\u683c\u91cc\u6240\u6709\u7f3a\u5931\u503c NaN&#xff0c;\u4e3a\u7a7a\u8fd4\u56de True&#xff0c;\u975e\u7a7a False<\/p>\n<p>print(df.isna())<\/p>\n<p>\u914d\u5957\u8865\u5145&#xff1a;notna() \u5224\u65ad\u975e\u7a7a\u3002<\/p>\n<h3>\u4e94\u3001\u6570\u503c\u7edf\u8ba1\u51fd\u6570&#xff1a;\u5355\u5217\u805a\u5408\u8ba1\u7b97<\/h3>\n<p>\u9488\u5bf9\u6570\u503c\u5217&#xff08;score\u3001age&#xff09;\u505a\u7edf\u8ba1&#xff0c;\u5168\u90e8\u662f\u5217\u7eb5\u5411\u8ba1\u7b97\u3002<\/p>\n<p>\u8868\u683c<\/p>\n<table>\n<tr>\u65b9\u6cd5\u4f5c\u7528\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>sum()<\/td>\n<td>\u6c42\u548c<\/td>\n<td>\u603b\u5206<\/td>\n<\/tr>\n<tr>\n<td>max()<\/td>\n<td>\u6700\u5927\u503c<\/td>\n<td>\u6700\u9ad8\u5206<\/td>\n<\/tr>\n<tr>\n<td>min()<\/td>\n<td>\u6700\u5c0f\u503c<\/td>\n<td>\u6700\u4f4e\u5206<\/td>\n<\/tr>\n<tr>\n<td>mean()<\/td>\n<td>\u5e73\u5747\u503c<\/td>\n<td>\u5e73\u5747\u5206<\/td>\n<\/tr>\n<tr>\n<td>median()<\/td>\n<td>\u4e2d\u4f4d\u6570<\/td>\n<td>\u4e2d\u95f4\u5206&#xff0c;\u4e0d\u53d7\u6781\u7aef\u503c\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>std()<\/td>\n<td>\u6807\u51c6\u5dee<\/td>\n<td>\u6570\u636e\u6ce2\u52a8\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>var()<\/td>\n<td>\u65b9\u5dee<\/td>\n<td>\u79bb\u6563\u7a0b\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>quantile(0.25)<\/td>\n<td>\u56db\u5206\u4f4d\u6570<\/td>\n<td>25% \u5206\u4f4d\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>print(&#034;\u5206\u6570\u6c42\u548c&#xff1a;&#034;,df[&#039;score&#039;].sum())<br \/>\nprint(&#034;\u6700\u9ad8\u5206&#xff1a;&#034;,df[&#039;score&#039;].max())<br \/>\nprint(&#034;\u6700\u4f4e\u5206&#xff1a;&#034;,df[&#039;score&#039;].min())<br \/>\nprint(&#034;\u5e73\u5747\u5206&#xff1a;&#034;,df.score.mean())<br \/>\nprint(&#034;\u4e2d\u4f4d\u6570&#xff1a;&#034;,df.score.median())<br \/>\nprint(&#034;\u6807\u51c6\u5dee&#xff1a;&#034;,df.score.std())<br \/>\nprint(&#034;\u65b9\u5dee&#xff1a;&#034;,df.score.var())<br \/>\nprint(&#034;25%\u5206\u4f4d\u6570&#xff1a;&#034;,df.score.quantile(0.25))<\/p>\n<h4>\u4e00\u952e\u5168\u91cf\u7edf\u8ba1 describe ()<\/h4>\n<p>\u65e0\u9700\u9010\u4e2a\u8c03\u7528&#xff0c;\u4e00\u6b21\u6027\u8f93\u51fa&#xff1a;\u8ba1\u6570\u3001\u5747\u503c\u3001\u6700\u503c\u3001\u56db\u5206\u4f4d\u6570&#xff0c;\u6570\u636e\u5206\u6790\u5fc5\u7528&#xff1a;<\/p>\n<p>print(df.describe())<\/p>\n<h3>\u516d\u3001\u8ba1\u6570\u7edf\u8ba1&#xff1a;count () \/value_counts ()<\/h3>\n<li>df.count()&#xff1a;\u7edf\u8ba1\u6bcf\u4e00\u5217\u975e\u7a7a\u6709\u6548\u5355\u5143\u683c\u6570\u91cf<\/li>\n<li>df[&#039;\u5217\u540d&#039;].value_counts()&#xff1a;\u7edf\u8ba1\u4e00\u5217\u4e2d\u6bcf\u4e2a\u503c\u51fa\u73b0\u7684\u9891\u6b21&#xff08;\u7edf\u8ba1\u91cd\u590d\u6b21\u6570\u795e\u5668&#xff09;<\/li>\n<p># \u5404\u5217\u6709\u6548\u6570\u636e\u884c\u6570<br \/>\nprint(&#034;\u6bcf\u4e00\u5217\u975e\u7f3a\u5931\u503c\u4e2a\u6570&#xff1a;\\\\n&#034;,df.count())<br \/>\n# \u5168\u8868\u884c\u91cd\u590d\u9891\u6b21<br \/>\nprint(&#034;\u5168\u8868\u91cd\u590d\u9891\u6b21&#xff1a;\\\\n&#034;,df.value_counts())<br \/>\n# \u5e38\u7528&#xff1a;\u7edf\u8ba1\u6bcf\u4e2a\u5e74\u9f84\u51fa\u73b0\u591a\u5c11\u6b21<br \/>\nprint(df[&#039;age&#039;].value_counts())<\/p>\n<h3>\u4e03\u3001\u91cd\u590d\u503c\u5904\u7406&#xff1a;duplicated () \/drop_duplicates ()<\/h3>\n<li>duplicated()&#xff1a;\u6807\u8bb0\u91cd\u590d\u884c&#xff0c;\u91cd\u590d\u8fd4\u56de True&#xff0c;\u9ed8\u8ba4\u6574\u884c\u5b8c\u5168\u4e00\u81f4\u624d\u7b97\u91cd\u590d&#xff1b;\u53ef\u6307\u5b9a\u5217\u5224\u65ad\u91cd\u590d<\/li>\n<li>drop_duplicates()&#xff1a;\u76f4\u63a5\u5220\u9664\u91cd\u590d\u884c&#xff0c;\u8fd4\u56de\u53bb\u91cd\u540e\u7684\u65b0\u8868\u683c<\/li>\n<p># \u5224\u65ad\u6574\u884c\u662f\u5426\u91cd\u590d<br \/>\nprint(&#034;\u662f\u5426\u6574\u884c\u91cd\u590d&#xff1a;\\\\n&#034;,df.duplicated())<br \/>\n# \u53ea\u6839\u636eage\u5217\u5224\u65ad\u91cd\u590d&#xff0c;\u5e74\u9f84\u76f8\u540c\u5373\u5224\u5b9a\u91cd\u590d<br \/>\nprint(&#034;age\u5217\u91cd\u590d\u5224\u65ad&#xff1a;\\\\n&#034;,df.duplicated(subset&#061;&#039;age&#039;))<br \/>\n# \u5220\u9664\u6240\u6709\u91cd\u590d\u884c<br \/>\nprint(&#034;\u53bb\u91cd\u540e\u6570\u636e&#xff1a;\\\\n&#034;,df.drop_duplicates())<\/p>\n<h3>\u516b\u3001\u968f\u673a\u62bd\u6837 sample ()<\/h3>\n<p>\u4ece\u8868\u683c\u968f\u673a\u62bd\u53d6\u82e5\u5e72\u884c&#xff0c;\u505a\u6570\u636e\u968f\u673a\u91c7\u6837&#xff0c;\u652f\u6301\u62bd 1 \u884c\u3001\u591a\u884c\u3002<\/p>\n<p># \u9ed8\u8ba4\u968f\u673a\u62bd1\u884c<br \/>\nprint(&#034;\u968f\u673a\u62bd1\u884c&#xff1a;\\\\n&#034;,df.sample())<br \/>\n# \u968f\u673a\u62bd3\u884c<br \/>\nprint(&#034;\u968f\u673a\u62bd3\u884c&#xff1a;\\\\n&#034;,df.sample(3))<\/p>\n<h3>\u4e5d\u3001\u6570\u636e\u6392\u5e8f&#xff1a;sort_index \/sort_values\/nlargest<\/h3>\n<h4>1. sort_index&#xff1a;\u6309\u7167\u884c\u7d22\u5f15\u6807\u7b7e\u6392\u5e8f<\/h4>\n<p>ascending&#061;False \u5012\u5e8f&#xff0c;True \u6b63\u5e8f<\/p>\n<p># \u884c\u7d22\u5f15a~f\u5012\u5e8f\u6392\u5217<br \/>\nprint(&#034;\u7d22\u5f15\u964d\u5e8f&#xff1a;\\\\n&#034;,df.sort_index(ascending&#061;False))<\/p>\n<h4>2. sort_values&#xff1a;\u6309\u5217\u7684\u503c\u6392\u5e8f&#xff08;\u6700\u5e38\u7528&#xff09;<\/h4>\n<p>\u652f\u6301\u591a\u5217\u8054\u5408\u6392\u5e8f&#xff0c;\u53ef\u4ee5\u7ed9\u4e0d\u540c\u5217\u8bbe\u7f6e\u4e0d\u540c\u5347\u964d\u5e8f\u89c4\u5219<\/p>\n<p># \u5148\u6309score\u5347\u5e8f&#xff0c;\u5206\u6570\u76f8\u540c\u5219age\u964d\u5e8f<br \/>\ndf_sorted &#061; df.sort_values(by&#061;[&#039;score&#039;,&#039;age&#039;],ascending&#061;[True,False])<br \/>\nprint(&#034;\u591a\u5217\u6df7\u5408\u6392\u5e8f&#xff1a;\\\\n&#034;,df_sorted)<\/p>\n<h4>3. nlargest \/nsmallest&#xff1a;\u5feb\u901f\u53d6\u6700\u503c\u524d N \u884c<\/h4>\n<p>\u4e0d\u7528\u6392\u5e8f\u518d head&#xff0c;\u4e00\u6b65\u62ff\u5230\u5206\u6570\u6700\u9ad8 2 \u6761<\/p>\n<p># \u53d6score\u3001age\u7efc\u5408\u6700\u5927\u76842\u884c<br \/>\nprint(df.nlargest(2,columns&#061;[&#039;score&#039;,&#039;age&#039;]))<br \/>\n# \u914d\u5957&#xff1a;nsmallest\u53d6\u6700\u5c0fN\u884c<br \/>\ndf.nsmallest(2,columns&#061;&#034;score&#034;)<\/p>\n<h3>\u5341\u3001\u5b8c\u6574\u914d\u5957\u7ec3\u4e60\u4ee3\u7801<\/h3>\n<p>import pandas as pd<\/p>\n<p>df &#061; pd.DataFrame({<br \/>\n    &#034;name&#034;:[&#034;tom&#034;,&#034;tom&#034;,&#034;jack&#034;,&#034;alice&#034;,&#034;bob&#034;,&#034;allen&#034;],<br \/>\n    &#034;age&#034;:[15,15,15,20,26,30],<br \/>\n    &#034;score&#034;:[60.5,60.5,80,30.6,70,83.5]<br \/>\n},<br \/>\nindex&#061;[&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;,&#039;e&#039;,&#039;f&#039;],<br \/>\ncolumns&#061;[&#034;name&#034;,&#034;score&#034;,&#034;age&#034;])<\/p>\n<p># \u5934\u5c3e\u67e5\u770b<br \/>\nprint(&#034;head\u524d3\u884c&#xff1a;\\\\n&#034;,df.head(3))<br \/>\nprint(&#034;tail\u540e3\u884c&#xff1a;\\\\n&#034;,df.tail(3))<\/p>\n<p># \u5217\u9009\u53d6<br \/>\nprint(&#034;\\\\n\u5355\u5217\u4e09\u79cd\u5199\u6cd5&#034;)<br \/>\nprint(df[&#039;name&#039;])<br \/>\nprint(df.name)<br \/>\nprint(df[[&#039;name&#039;]])<br \/>\nprint(&#034;\\\\n\u591a\u5217&#043;\u5934\u5c3e&#034;,df[[&#039;name&#039;,&#039;score&#039;]].head(3))<\/p>\n<p># \u5e03\u5c14\u7b5b\u9009<br \/>\nprint(&#034;\\\\n\u5206\u6570\u5927\u4e8e70&#034;,df[df.score&gt;70])<br \/>\nprint(&#034;\\\\n\u5206\u6570&gt;70\u4e14\u5e74\u9f84&lt;30&#034;,df[(df.score&gt;70)&amp;(df.age&lt;30)])<\/p>\n<p># \u5339\u914d\u3001\u7a7a\u503c<br \/>\nprint(&#034;\\\\nisin\u5339\u914djack&#034;,df.isin([&#034;jack&#034;]))<br \/>\nprint(&#034;\\\\n\u7a7a\u503c\u5224\u65ad&#034;,df.isna())<\/p>\n<p># \u7edf\u8ba1\u6307\u6807<br \/>\nprint(&#034;\\\\n\u603b\u5206&#034;,df[&#039;score&#039;].sum())<br \/>\nprint(&#034;\u5e73\u5747\u5206&#034;,df.score.mean())<br \/>\nprint(df.describe())<\/p>\n<p># \u8ba1\u6570\u4e0e\u53bb\u91cd<br \/>\nprint(&#034;\\\\n\u6709\u6548\u884c\u6570&#034;,df.count())<br \/>\nprint(&#034;\\\\n\u91cd\u590d\u5224\u65ad&#034;,df.duplicated())<br \/>\nprint(&#034;\\\\n\u53bb\u91cd&#034;,df.drop_duplicates())<\/p>\n<p># \u62bd\u6837<br \/>\nprint(&#034;\\\\n\u968f\u673a\u62bd\u6837&#034;,df.sample(2))<\/p>\n<p># \u6392\u5e8f<br \/>\nprint(&#034;\\\\n\u7d22\u5f15\u5012\u5e8f&#034;,df.sort_index(ascending&#061;False))<br \/>\nprint(&#034;\\\\n\u591a\u5217\u6392\u5e8f&#034;,df.sort_values(by&#061;[&#034;score&#034;],ascending&#061;True))<br \/>\nprint(&#034;\\\\n\u5206\u6570\u6700\u9ad82\u884c&#034;,df.nlargest(2,&#034;score&#034;))<\/p>\n<h3>\u5341\u4e00\u3001\u65b0\u624b\u4f7f\u7528\u603b\u7ed3<\/h3>\n<li>\u9884\u89c8\u6570\u636e&#xff1a;head\/tail&#xff1b;\u9009\u5217\u7528[]&#xff0c;\u591a\u5217\u5fc5\u987b\u5957\u5217\u8868&#xff1b;<\/li>\n<li>\u7cbe\u51c6\u7b5b\u9009\u4f18\u5148\u7528\u5e03\u5c14\u7d22\u5f15&#xff0c;\u591a\u6761\u4ef6\u8bb0\u4f4f\u62ec\u53f7 &#043;&amp;\/|&#xff1b;<\/li>\n<li>\u505a\u7edf\u8ba1&#xff1a;\u5355\u5217\u7528 sum\/mean \u7b49&#xff0c;\u5168\u8868\u4e00\u952e\u7528describe&#xff1b;<\/li>\n<li>\u91cd\u590d\u6e05\u7406&#xff1a;duplicated\u67e5\u91cd\u590d&#xff0c;drop_duplicates\u5220\u91cd\u590d&#xff1b;<\/li>\n<li>\u6392\u5e8f\u5206\u4e24\u7c7b&#xff1a;\u6309\u7d22\u5f15 sort_index&#xff0c;\u6309\u5b57\u6bb5 sort_values&#xff1b;\u53d6\u6781\u503c\u7528 nlargest&#xff1b;<\/li>\n<li>\u968f\u673a\u62ff\u6570\u636e\u76f4\u63a5 sample&#xff0c;\u9002\u5408\u62bd\u6837\u6d4b\u8bd5\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\u4f7f\u7528 pandas \u505a\u6570\u636e\u5206\u6790&#xff0c;\u9664\u4e86 DataFrame \u57fa\u7840\u521b\u5efa\u3001\u7d22\u5f15\u53d6\u503c&#xff0c;\u7edd\u5927\u591a\u6570\u5de5\u4f5c\u90fd\u4f9d\u8d56\u5404\u7c7b\u5185\u7f6e\u65b9\u6cd5&#xff1a;\u67e5\u770b\u5934\u5c3e\u3001\u5217\u7b5b\u9009\u3001\u5e03\u5c14\u8fc7\u6ee4\u3001\u7edf\u8ba1\u8ba1\u7b97\u3001\u53bb\u91cd\u3001\u6392\u5e8f\u3001\u968f\u673a\u62bd\u6837\u7b49\u3002\u672c\u7bc7\u57fa\u4e8e\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u5206\u95e8\u522b\u7c7b\u8bb2\u89e3\u9ad8\u9891\u65b9\u6cd5&#xff0c;\u9002\u914d\u6570\u636e\u5206\u6790\u65e5\u5e38\u4f7f\u7528\u3002\u63d0\u524d\u56fa\u5b9a\u6f14\u793a\u6570\u636e\u96c6&#xff0c;\u5168\u6587\u6240\u6709\u6848\u4f8b\u57fa\u4e8e\u8fd9\u4efd\u8868\u683c\u8fd0\u884c&#xff1a;import pandas as pddf  pd.DataFrame({\\&#8221;name\\&#8221;:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-84339","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/84339.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u524d\u8a00\u4f7f\u7528 pandas \u505a\u6570\u636e\u5206\u6790&#xff0c;\u9664\u4e86 DataFrame \u57fa\u7840\u521b\u5efa\u3001\u7d22\u5f15\u53d6\u503c&#xff0c;\u7edd\u5927\u591a\u6570\u5de5\u4f5c\u90fd\u4f9d\u8d56\u5404\u7c7b\u5185\u7f6e\u65b9\u6cd5&#xff1a;\u67e5\u770b\u5934\u5c3e\u3001\u5217\u7b5b\u9009\u3001\u5e03\u5c14\u8fc7\u6ee4\u3001\u7edf\u8ba1\u8ba1\u7b97\u3001\u53bb\u91cd\u3001\u6392\u5e8f\u3001\u968f\u673a\u62bd\u6837\u7b49\u3002\u672c\u7bc7\u57fa\u4e8e\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u5206\u95e8\u522b\u7c7b\u8bb2\u89e3\u9ad8\u9891\u65b9\u6cd5&#xff0c;\u9002\u914d\u6570\u636e\u5206\u6790\u65e5\u5e38\u4f7f\u7528\u3002\u63d0\u524d\u56fa\u5b9a\u6f14\u793a\u6570\u636e\u96c6&#xff0c;\u5168\u6587\u6240\u6709\u6848\u4f8b\u57fa\u4e8e\u8fd9\u4efd\u8868\u683c\u8fd0\u884c&#xff1a;import pandas as pddf pd.DataFrame({&quot;name&quot;:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/84339.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-26T03:42:31+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/84339.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/84339.html\",\"name\":\"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-26T03:42:31+00:00\",\"dateModified\":\"2026-07-26T03:42:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/84339.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/84339.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/84339.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/84339.html","og_locale":"zh_CN","og_type":"article","og_title":"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u524d\u8a00\u4f7f\u7528 pandas \u505a\u6570\u636e\u5206\u6790&#xff0c;\u9664\u4e86 DataFrame \u57fa\u7840\u521b\u5efa\u3001\u7d22\u5f15\u53d6\u503c&#xff0c;\u7edd\u5927\u591a\u6570\u5de5\u4f5c\u90fd\u4f9d\u8d56\u5404\u7c7b\u5185\u7f6e\u65b9\u6cd5&#xff1a;\u67e5\u770b\u5934\u5c3e\u3001\u5217\u7b5b\u9009\u3001\u5e03\u5c14\u8fc7\u6ee4\u3001\u7edf\u8ba1\u8ba1\u7b97\u3001\u53bb\u91cd\u3001\u6392\u5e8f\u3001\u968f\u673a\u62bd\u6837\u7b49\u3002\u672c\u7bc7\u57fa\u4e8e\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u5206\u95e8\u522b\u7c7b\u8bb2\u89e3\u9ad8\u9891\u65b9\u6cd5&#xff0c;\u9002\u914d\u6570\u636e\u5206\u6790\u65e5\u5e38\u4f7f\u7528\u3002\u63d0\u524d\u56fa\u5b9a\u6f14\u793a\u6570\u636e\u96c6&#xff0c;\u5168\u6587\u6240\u6709\u6848\u4f8b\u57fa\u4e8e\u8fd9\u4efd\u8868\u683c\u8fd0\u884c&#xff1a;import pandas as pddf pd.DataFrame({\"name\":","og_url":"https:\/\/www.wsisp.com\/helps\/84339.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-26T03:42:31+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/84339.html","url":"https:\/\/www.wsisp.com\/helps\/84339.html","name":"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-26T03:42:31+00:00","dateModified":"2026-07-26T03:42:31+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/84339.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/84339.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/84339.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Pandas DataFrame \u5e38\u7528\u65b9\u6cd5\u5168\u6c47\u603b\uff08\u5165\u95e8\u5b9e\u64cd\u535a\u6587\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/84339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=84339"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/84339\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=84339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=84339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=84339"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=84339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}