簡體   English   中英

如何在for循環中合並數據幀

[英]How to combine the dataframes in a for loop

我用以下代碼完成了網絡抓取:

Number = soup.find('th',text = "Number of samples").find_next_sibling("td").text


for x in range(1,int(Number)+1):            #loop of function to parse the data format I want
    item = item_text.split('tooltip')[x].split("class")[0].replace('"','').replace(',','').replace(':','').replace("<br>"," ").replace("/","").replace("\\","")
    #print(item) 

    TESTDATA=StringIO(item)

    df = pd.read_csv(TESTDATA, sep=" ",header=None) 
    print(df)

現在結果如下:

                0     1   2      3    4         5   6      7     8    9   \
0  TCGA-KK-A7B3-01A  Male NaN  Stage  not  reported NaN  Alive  FPKM  5.5  
       10    11   12    13      14
0  Living  days  899  (2.5  years)
               0     1    2      3    4         5   6      7     8     9   \
0  TCGA-G9-6347-01A  Male NaN  Stage  not  reported NaN  Alive  FPKM  14.2 
       10    11    12    13      14
0  Living  days  2089  (5.7  years) 
...

現在的問題是如何將那些單獨的數據幀合並為一個數據幀,以便更輕松地保存到整個csv文件中?

謝謝

使用pd.concat

all_dataframes = []

for x in range(1,int(Number)+1):
    ....

    df = pd.read_csv(TESTDATA, sep=" ",header=None) 
    all_dataframes.append(df)

concat_df = pd.concat(all_dataframes)

暫無
暫無

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

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