2016-12-27 84 views
1

我有這樣一個CSV文件,列出以下幾點:熊貓分配多個CSV值在單獨的數據幀列

#1, #2, #3, #4, #5, #6 

我要生成使用read_csv方法的數據幀,但我怎麼分配值在我的數據框的前5列到單列作爲列表?我怎麼能申請一個標題到那個專欄?

回答

2

創建一個新的數據框。

假如你以前的數據框對象df=read_csv('something.csv')

df1 = pd.DataFrame(columns=['ColName1','ColName2']) #new DataFrame with column Names as ColName1 and ColName2 
c=0 #Index for new DataFrame 
for i in range(len(df)): 
    df1.loc[c] = [None for x in range(2)] #Row elements initially Null 
    df1.loc[c].ColName1 = [] 
    for j in range(1,5): 
     df1.loc[c].ColName1.append(df.loc[i]['#' + str(j)]) 
    df1.loc[c].ColName2 = df.loc[i]['#6'] 
    c += 1 
df1.to_csv('new.csv') 
+0

有沒有辦法簡單地做,到read_csv功能作爲參數調用? – Jasonca1

+0

你可以,但說實話,並不那麼簡單。 –

+0

我能否爲我自己的好奇而看到它? – Jasonca1