[英]Load columns x1,x2,x3.. with headers: 0,1,
我是python的新手,並且遇到了一個非常令人沮喪的問題。 我需要加載一個csv文件的第1-12列(因此不是第0列),但是我需要跳過excel的標題,並用"0,1,..,11"
覆蓋它,我需要使用panda.read_csv()為此。
基本上,我的csv是:
"a", "b", "c", ..., "l"
1, 2, 3, ..., 12
1, 2, 3, ..., 12
我想將其作為數據框加載
dataframe[0] = 2,2,2,..
dataframe[1] = 3,3,3..
ergo跳過第一列,並使數據幀從索引0開始。我嘗試設置usecols = [1,2,3..]
,但是索引為1,2,3,..
任何幫助將不勝感激。
您可以使用header=(int)
刪除標題行,使用usecols=range(1,12)
抓取最后11列,使用names=range(11)
命名11列(從0到10)。
這是一個偽數據集:
This is the header. Header header header.
And the second header line.
a,b,c,d,e,f,g,h,i,j,k,l
1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,10,11,12
使用代碼:
> df = pd.read_csv('data_file.csv', usecols=range(1,12), names=range(11), header=2)
> df
# returns:
0 1 2 3 4 5 6 7 8 9 10
0 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
2 2 3 4 5 6 7 8 9 10 11 12
> df[0]
# returns:
0 2
1 2
2 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.