簡體   English   中英

Python Pandas“未命名”列不斷出現

[英]Python Pandas 'Unnamed' column keeps appearing

我遇到一個問題,每次我運行程序(從.csv文件讀取數據幀)時,都會出現一個名為“未命名”的新列。

運行3次后采樣輸出列-

  Unnamed: 0  Unnamed: 0.1            Subreddit  Appearances

這是我的代碼。 對於每一行,“未命名”列僅增加1。

df = pd.read_csv(Location)
while counter < 50:
    #gets just the subreddit name
    e = str(elem[counter].get_attribute("href"))
    e = e.replace("https://www.reddit.com/r/", "")
    e = e[:-1]
    if e in df['Subreddit'].values:
        #adds 1 to Appearances if the subreddit is already in the DF
        df.loc[df['Subreddit'] == e, 'Appearances'] += 1
    else:
        #adds new row with the subreddit name and sets the amount of appearances to 1.
        df = df.append({'Subreddit': e, 'Appearances': 1}, ignore_index=True)
    df.reset_index(inplace=True, drop=True)
    print(e)
    counter = counter + 2
#(doesn't work) df.drop(df.columns[df.columns.str.contains('Unnamed', case=False)], axis=1)

我第一次使用干凈的.csv文件運行它時,它運行完美,但是每次之后,都會出現另一個“未命名”列。 我只是想每次都顯示“ Subreddit”和“ Appearances”列。

另一種解決方案是讀取屬性為index_col=0 csv,而不考慮索引列: df = pd.read_csv(Location, index_col=0)

每次我運行程序(...)時,都會出現一個名為“未命名”的新列。

我想那是由於reset_index引起的,或者您的代碼中某處有一個to_csv ,如@jpp建議的那樣。 要修復to_csv確保使用index=False

df.to_csv(path, index=False)

通常,這是我將如何處理您的任務。 這樣做是首先對所有外觀進行計數(由e ),然后從這些計數中創建一個新的數據框,以與您已有的數據框合並( how='outer'添加尚不存在的行)。 這樣避免了為每個元素重置索引,從而避免了該問題,並且性能更高。

以下是包含這些想法的代碼:

base_df = pd.read_csv(location)
appearances = Counter()  # from collections
while counter < 50:
    #gets just the subreddit name
    e = str(elem[counter].get_attribute("href"))
    e = e.replace("https://www.reddit.com/r/", "")
    e = e[:-1]
    appearances[e] += 1
    counter = counter + 2
appearances_df = pd.DataFrame({'e': e, 'appearances': c } 
                               for e, c in x.items())
df = base_df.merge(appearances_df, how='outer', on='e')

暫無
暫無

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

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