2015-09-09 32 views
1

我有一個csv文件的魚的事件,需要修剪出現只有一次的魚,然後輸出這個'修剪'csv。然而,我正在使用的函數添加一個無標題的列到裁剪後的csv中,這進一步計算了我需要處理修剪後的文件。如何阻止大熊貓創建新列?

該列包含行號從to_keep,我相信是由此行創建的:return df[df[colname].isin(to_keep)]。我想讓這個腳本根本不創建這個列;否則我沒有手動刪除它從我修剪的每一個csv文件!

import pandas as pd 

def trim_single_entries(fn, colname): 
# remove all entries where colname's entry is unique to one row across the whole file 
df = pd.read_csv(fn) 
if colname in df.columns: 
    counts = df[colname].value_counts() 
    to_keep = [counts.index[i] for i in range(0,len(counts)) if counts.values[i] > 1] 
    return df[df[colname].isin(to_keep)] 
else: 
    return False 

x = trim_single_entries('fish_data.csv', 'catalognumber') 

x.to_csv('trimmed_fish_data.csv') 
+1

加上'指數= FALSE'到'to_csv'方法 –

+0

如果你回答這個問題,我將其標記爲正確的! – spops

+0

已回答問題? http://stackoverflow.com/questions/20845213/how-to-avoid-python-pandas-creating-an-index-in-a-saved-csv – tmthyjames

回答

1

添加index=Falseto_csv方法