我正在嘗試將數據框導出到現有的格式化 csv 文件,但數據框繼續以垂直形式附加,以及應該水平的附加標題。A B C D E F1 2 3 4 5 6 #=-> This is the format I have in my exisiting csv fileA B C D E F1 2 3 4 5 6x x x x x x #-> This is how I want to do itA B C D E F1 2 3 4 5 6A 1B 2C 3 D 4 #-> This is what's currently happening任何幫助將非常感激。謝謝!df.iloc[location].to_csv(path,mode='a')這是我一直在嘗試的代碼。
3 回答

桃花長相依
TA貢獻1860條經驗 獲得超8個贊
您希望發生這種情況的情況尚不清楚,但一種好的通用方法是
df1 = pd.read_csv(...) # Read the file.
df2 = pd.DataFrame(...) # Make a DataFrame of the new entries you want to add.
df = pd.concat([df1, df2]) # Concatenate the two.
df.to_csv(...) # Write the file to the original directory.

BIG陽
TA貢獻1859條經驗 獲得超6個贊
df.iloc[location].T.to_csv('filename.csv',mode='a',index=False,header=False)

繁星淼淼
TA貢獻1775條經驗 獲得超11個贊
df.iloc[location]
可以給你Series
哪些不同的待遇DataFrame
您必須將其轉換為DataFrame
但使用列表Series
來獲取行而不是列中的數據
df = pd.DataFrame( [df.iloc[location]] )
然后保存到沒有標題的csv
df.to_csv(path, mode='a', header=None)
編輯:您也可以轉換Series
為DataFrame
使用.to_frame()
,然后使用.T
將列轉置為行
df = df.iloc[location].to_frame().T
添加回答
舉報
0/150
提交
取消