![](/img/trans.png)
[英]pandas read_csv does not capture final (unnamed) column into dataframe
[英]Cannot drop an unnamed column in a csv in pandas
嘗試從 CSV 創建的數據框中刪除未命名的列,但不會刪除。
for m in mlist: #Loop through each month
print('Running merge for '+str(m)+' collecting csv files...')
csvlist = [i for i in glob.glob(str(m)+'2019_G*.{}'.format('csv'))] #List all the CSVs of each month
month_csv = pd.concat([pd.read_csv(csv) for csv in csvlist])
month_csv = month_csv.astype({unique_gridid:'int'})
month_csv = month_csv.set_index(unique_gridid)
month_csv.columns.str.match("Unnamed")
month_csv.loc[:,~month_csv.columns.str.match("Unnamed")]
month_csv.drop("Unnamed: 0", axis=1)
print(month_csv)
控制台仍然在打印輸出中顯示未命名的列。
問題在於以下調用:
month_csv.drop("Unnamed: 0", axis=1)
drop
函數有一個inplace
參數,默認情況下,它等於False
。 因此,該函數返回一個帶有已刪除列的新數據框。 你有兩個選擇:
month_csv.drop("Unnamed: 0", axis=1, inplace=True)
或者
month_csv = month_csv.drop("Unnamed: 0", axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.