[英]Save, for each row in df, N-1 columns into a new line and the N-th column into another line (pandas)
我有一個非常大的 dataframe,我正在尋找一種有效的方法將其存儲為格式化的文本文件,如下所示。
考慮以下示例 dataframe:
In [2]: df
Out[2]:
id etc data
0 12 info12 text12
1 34 info34 text34
desired output in txt:
# 12 info12
text12
# 34 info34
text34
有沒有一種快速的方法可以做到這一點並避免多種低效for
?
pandas 有一個內置的方法。
它看起來像這樣:
# some large dataframe
df = pd.DataFrame([[1,1,1],[2,2,2],[3,3,3]],columns=['a','b','c'])
# save as a csv file
df.to_csv('file_name.csv')
......這就是你所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.