<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Pandas處理8w條數(shù)據(jù),最終生成800w條!

          共 2736字,需瀏覽 6分鐘

           ·

          2021-09-28 16:15

          點擊上方“Python爬蟲與數(shù)據(jù)挖掘”,進(jìn)行關(guān)注

          回復(fù)“書籍”即可獲贈Python從入門到進(jìn)階共10本電子書

          野云萬里無城郭,雨雪紛紛連大漠。

          粉絲需求說明

          今天一大早,就有一位粉絲,讓我處理一個Padas問題,詳情請看下圖:

          f8fe43f8218811a39f5912dd5c6b4cb0.webp

          哎,他這個敘述確實有點難懂,我來翻譯一下。

          需求:有一個csv文件,里面有一些經(jīng)緯度數(shù)據(jù)。其中每一行有若干組經(jīng)緯度數(shù)據(jù),都是用逗號隔開。我們需要做的就是,將每一個經(jīng)緯度數(shù)據(jù)提取出來,分別存儲到Excel的兩列中,同時多添加一列,表示行號,總共就是3列。

          始數(shù)據(jù)截圖:

          dfe9a688284421fa7e086e74af954c60.webp

          我處理后截圖:

          8170d46e0ac564e70106c7dea871930d.webp

          我的測試

          直接先上完整代碼吧!

          import?pandas?as?pd
          #?1.?讀取數(shù)據(jù)
          df?=?pd.read_csv("wgs84?-?副本.csv",header=None)
          df.columns?=?["列一","列二"]
          ##?2.?數(shù)據(jù)處理與數(shù)據(jù)寫入
          df1?=?pd.DataFrame()
          for?index,value?in?enumerate(df["列一"]):
          ????y?=?value[10:-3].split(",")
          ????z?=?[[index+1]?+?i.strip().split("?")?for?i?in?y]
          ????df1?=?df1.append(z)
          df1.columns?=?["行號","經(jīng)度","維度"]
          ##?3.?保存數(shù)據(jù)
          df1.to_excel("經(jīng)緯度.xlsx",index=None)

          上述代碼共分為3個部分,詳細(xì)解釋如下:

          • 首先是數(shù)據(jù)讀取。由于給我的數(shù)據(jù)沒有標(biāo)題行,讀取時候需要使用header=None參數(shù),同時為了后續(xù)處理方便,為數(shù)據(jù)添加標(biāo)題行。
          • 接著是數(shù)據(jù)處理和數(shù)據(jù)寫入。由于每一行數(shù)據(jù)都在(( ))雙括號中,我們需要先提取其中的字符串,然后按照,逗號切分,再按照空格切分,得到每個經(jīng)緯度數(shù)據(jù)的列表。同時使用+加號拼接列表,就得到了每行數(shù)據(jù),都是3個數(shù)據(jù)組成的列表嵌套。再使用append()函數(shù),就可以將數(shù)據(jù)添加到表格中。
          • 最后是數(shù)據(jù)寫入。我們將組織好的數(shù)據(jù),最終寫入到Excel文件中,不要索引行,因此使用了index=None參數(shù)。

          為了更加清晰的展現(xiàn)這個效果,下面提取其中一條數(shù)據(jù)為大家講解此過程。

          ① 取出其中一條數(shù)據(jù)

          x?=?df["列一"][0]
          x

          結(jié)果如下:

          325ab67d3d65aaa12f445a1885f66479.webp

          ② 切片+切分字符串

          y?=?x[10:-3].split(",")
          y

          結(jié)果如下:

          987217562e10155ded78277088e0fb7f.webp

          注意到上述結(jié)果中,存在一些空格,我們后續(xù)需要使用strip()函數(shù)去除掉。

          ③ 列表拼接+列表解析式

          z?=?[[1]?+?i.strip().split("?")?for?i?in?y]
          z

          結(jié)果如下:

          519ecdd807183ab4a72a7181ce47bb9b.webp

          ④ 使用append函數(shù)添加數(shù)據(jù)

          df1?=?pd.DataFrame()
          df1.append(z)

          結(jié)果如下:

          5bd958c4df177a9aa1fe730ad37ab238.webp

          如果有8w+數(shù)據(jù),怎么處理的快呢?

          在請教了明佬以后,最終給出如下代碼,僅需要十幾秒,就可以處理8w+條數(shù)據(jù)。
          import?pandas?as?pd
          import?os
          # 分塊、分塊、分塊
          chunks?=?pd.read_csv("wgs84.csv",?chunksize=10000)
          last?=?0
          for?df?in?chunks:
          ????df.dropna(how="all",?inplace=True)
          ????result?=?[]
          ????for?a,?b?in?df.values:
          ????????if?not?isinstance(a,?str):
          ????????????continue
          ????????if?pd.isna(b):
          ????????????b?=?last+1
          ????????elif?isinstance(b,?str)?and?not?b.isdigit():
          ????????????print(a,?b,?last)
          ????????????continue
          ????????for?p?in?a[10:-3].split(","):
          ????????????result.append((b,?*p.split()))
          ????????if?not?pd.isna(b):
          ????????????last?=?int(b)
          ????df?=?pd.DataFrame(result,?columns=["行號",?"經(jīng)度",?"維度"])
          ????file?=?"result.csv"
          ????df.to_csv(file,?index=False,?mode='a',
          ??????????????header=not?os.path.exists(file),?encoding="u8")

          8w+的數(shù)據(jù),最終生成后,卻有800w條,Excel是打不開的。

          7e91c3874c2d78f0261dde57a58231c5.webp

          為了大家能夠更快的理解這段代碼,有幾個知識點,我給大家講解一下。

          ① 列表前面加個*號,啥意思?

          列表、元組前面加星號,作用是將列表解開成兩個獨立的參數(shù),傳入函數(shù),字典前面加兩個星號,是將字典解開成獨立的元素作為形參。
          x?=?"Polygon?((100.41370674?38.56406437,?100.41363237?38.56400317))"
          #?不加*號
          for?i,p?in?enumerate(x[10:-3].split(",")):
          ????print(p.split())
          #?加*號
          for?i,p?in?enumerate(x[10:-3].split(",")):
          ????print(*p.split())

          結(jié)果如下:

          3a7c2bbb7f162ae06a14c7cd78a5348a.webp

          ② isinstance()函數(shù),啥意思?

          import?numpy?as?np

          a?=?2
          b?=?np.nan

          isinstance?(a,int)
          isinstance?(a,str)
          isinstance?(b,int)
          isinstance?(a,str)

          結(jié)果如下:

          c941a6a7ae05ae6e83a897359d7bd411.webp

          好了,本次答疑就到這里,你學(xué)會了嗎?
          如果是你,你會怎么做呢?

          -------------------?End?-------------------

          往期精彩文章推薦:

          8388a4cc1b6016591d48524756178537.webp

          歡迎大家點贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

          想加入Python學(xué)習(xí)群請在后臺回復(fù)【入群

          萬水千山總是情,點個【在看】行不行

          /今日留言主題/

          隨便說一兩句吧~

          瀏覽 31
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩午夜无码 | 波多野吉衣中文字幕在线 | 男男一区二区三区无码 | 三级在线观看 | 91无码免费电影 |