<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ***** dataframe與series中經常有文本格式的數據存在,pandas提供了良好的工具用來處理這些文本 ``` #用列表創建Series s = pd.Series(['A', 'B', 'C', 'Aaba ', ' Baca', 'CABA ', 'dog', 'cat']) ``` ![](https://img.kancloud.cn/be/58/be5811e1612a93d6223f222e391c4fe3_155x193.png) ``` #調用s.str將series-S變為String 然后再去除兩端空格 s.str.strip() # 去除空格 ``` ![](https://img.kancloud.cn/06/ab/06aba2e6f60e742d624fdd9aa1007041_166x193.png) ``` # 將字符串轉換成大寫 s.str.upper() ``` ![](https://img.kancloud.cn/90/9d/909d8ad42d5a2f5b9c4c40e14f2cb026_182x197.png) ``` #pandas中字符串函數可以連用,strip之后仍然是series需要再調用str變為字符串,并尋找結尾為a的字符串 s.str.strip().str.endswith("a") ``` ![](https://img.kancloud.cn/46/3f/463ff735463d7608c35f6dc6f41e6d1d_162x198.png) ``` #外面再套一個series,會返回結果為true的值 s[s.str.strip().str.endswith("a")] ``` ![](https://img.kancloud.cn/1b/a4/1ba45d1ccecf57ec042a1583a3eea5cb_180x76.png) ***** 一個很常用的場景就是當你的index或者column名稱前后包含了空格的時候,你可以用str的方法剔除這些空格,從而避免不必要的麻煩 ``` #注意age和name前后有空格 a = {"name ":["xiaoming","xiaohong","xiaogang"]," age":[12,13,14]} test = pd.DataFrame(data = a) ``` #此時列名age和name有空格,但看不出來 ![](https://img.kancloud.cn/79/25/7925ee332c02bb446f4e3c0881400241_331x219.png) 這時 ``` #會報錯,是test[' age'] test['age'] ``` 去掉列名前后空格,這次打印正確 ``` test.columns = test.columns.str.strip() test['age'] ``` ![](https://img.kancloud.cn/bb/26/bb2646cfe2095ef33e719f13ed0c5e19_248x84.png) ### 1.5.1. Splitting and Replacing String split方法用于根據某個分隔符對字符進行分割,返回一個列表 ``` #取Player列所有數據 df['Player'] ``` ![](https://img.kancloud.cn/35/06/35065d0761afe2fb6888f2939f2dc951_395x231.png) 目標將姓和名進行分割 ``` #.str將series變成字符串對象再用split 結果 df['Player'].str.split(" ") ``` ![](https://img.kancloud.cn/6f/73/6f7392c1bbbfae44efe4e244d02be236_316x212.png) ***** 取得Player列分割后所有列表的第一個元素(姓) ``` # 使用get方法獲取指定位置的元素 df['Player'].str.split(" ").str.get(1) ``` ![](https://img.kancloud.cn/e3/85/e3856036196e5b7916cd30e1b10ee3ca_233x228.png) ``` # 使用expand方法,將分割出的列表變為多個列,expand-擴展 df['Player'].str.split(" ",expand = True) ``` ![](https://img.kancloud.cn/aa/b1/aab18303d4d7e5d5ab3fb1b92018082d_238x236.png) ***** 使用\[\]對字符串的位置進行索引選取 ``` #.str應用在Player列的每一個元素上,將series的元素變為字符串,對字符串使用切片索引選取 df['Player'].str[:3] ``` ### 1.5.2. Extracting substring 通過正則表達式選取字符串中的子集
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看