简体   繁体   English

对于df中的每一行,将N-1列保存到新行中,将第N列保存到另一行中(熊猫)

[英]Save, for each row in df, N-1 columns into a new line and the N-th column into another line (pandas)

I have a very big dataframe and I am looking for an efficient way to store it as a formated text file as shown bellow.我有一个非常大的 dataframe,我正在寻找一种有效的方法将其存储为格式化的文本文件,如下所示。

Consider the following example 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

Is there a quick way to do this and avoid multiple inefficient for conditions?有没有一种快速的方法可以做到这一点并避免多种低效for

pandas has a built in method for this. pandas 有一个内置的方法。

it look like this:它看起来像这样:

# 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')

...that is all you need. ......这就是你所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM