[英]Python Pandas 'Unnamed' column keeps appearing
I am running into an issue where each time I run my program (which reads the dataframe from a .csv file) a new column shows up called 'Unnamed'. 我遇到一个问题,每次我运行程序(从.csv文件读取数据帧)时,都会出现一个名为“未命名”的新列。
sample output columns after running 3 times - 运行3次后采样输出列-
Unnamed: 0 Unnamed: 0.1 Subreddit Appearances
here is my code. 这是我的代码。 for each row, the 'Unnamed' columns simply increase by 1.
对于每一行,“未命名”列仅增加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)
The first time i run it, with a clean .csv file, it works perfect, but each time after, another 'Unnamed' column shoes up. 我第一次使用干净的.csv文件运行它时,它运行完美,但是每次之后,都会出现另一个“未命名”列。 I just wanted the 'Subreddit' and 'Appearances' columns to show each time.
我只是想每次都显示“ Subreddit”和“ Appearances”列。
另一种解决方案是读取属性为index_col=0
csv,而不考虑索引列: df = pd.read_csv(Location, index_col=0)
。
each time I run my program (...) a new column shows up called 'Unnamed'.
每次我运行程序(...)时,都会出现一个名为“未命名”的新列。
I suppose that's due to reset_index
or maybe you have a to_csv
somewhere in your code as @jpp suggested. 我想那是由于
reset_index
引起的,或者您的代码中某处有一个to_csv
,如@jpp建议的那样。 To fix the to_csv
be sure to use index=False
: 要修复
to_csv
确保使用index=False
:
df.to_csv(path, index=False)
In general, here's how I would approach your task. 通常,这是我将如何处理您的任务。 What this does is to count all appearances first (keyed by
e
), and from these counts create a new dataframe to merge with the one you already have ( how='outer'
adds rows that don't exist yet). 这样做是首先对所有外观进行计数(由
e
),然后从这些计数中创建一个新的数据框,以与您已有的数据框合并( how='outer'
添加尚不存在的行)。 This avoids resetting the index for each element which should avoid the problem and is also more performant. 这样避免了为每个元素重置索引,从而避免了该问题,并且性能更高。
Here's the code with these thoughts included: 以下是包含这些想法的代码:
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.