簡體   English   中英

如何將 pandas dataframe 划分為多個較小的數據幀或元組列表?

[英]How do i divide a pandas dataframe into multiple smaller dataframes or lists of tuples?

我正在使用 pandas.read_csv(path, low_memory=False) 將大型 csv 文件讀取到 memory 我想將某些行組逐行提取並插入數據庫中。 我知道第 11 行到第 62 行 go 到一個表中,第 65 行到第 10000 行 go 到另一個表中,有辦法從 Z6A8064B5DF479455557DZCC 中獲取行子集以循環遍歷 4794555500553。 如果行的元素 2 不是 nan,我還需要只處理子集中的數據。 謝謝你的幫助

您的問題有幾個解決方案。 來自pandas read_csv 文檔

跳過

Line numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2].

跳過頁腳

Number of lines at bottom of file to skip (Unsupported with engine='c').

Number of rows of file to read. Useful for reading pieces of large files.

對您來說最直觀的解決方案是

df1 = pd.read_csv(path, low_memory=False, skiprows=65, nrows=10000-65)

但當然你也可以 go for

df1 = pd.read_csv(path, low_memory=False, skiprows=65, skipfooter=total_rows-10000)

您可以簡單地使用:

dataframe_name['column_name'] (conditions) (value)

例子:

dataframe['row_num'] > 200

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM