简体   繁体   English

在cthon中向csv或excel文件添加一个空行

[英]Adding an empty row with group wise to csv or excel file in python

How to insert an empty line to separate each group in excel or csv. 如何插入一个空行来分隔excel或csv中的每个组。 Currently I am using pandas, I am not bale to do this. 目前我正在使用熊猫,我不是这样做的。

CURRENT TABLE: 当前表:

column1   |   column2   |  column3
----------------------------------
  A       |     23     |  blue
  A       |     23     |  orange
  A       |     45     |  yellow
  A       |     45     |  yellow
  A       |     45     |  blue
  A       |     60     |  green
  A       |     60     |  green
  A       |     75     |  pink

_ _

DESIRED TABLE 渴望的表

Note: the blank row after each distinct column1 注意:每个不同列1后面的空行

column1   |   column2   |  column3
----------------------------------
  A       |     23     |  blue
  A       |     23     |  orange

  A       |     45     |  yellow
  A       |     45     |  yellow
  A       |     45     |  blue

  A       |     60     |  green
  A       |     60     |  green

  A       |     75     |  pink

Can any one suggest me the way how it is achievable in python. 任何人都可以告诉我如何在python中实现它。

You can use groupby with custom function where add last empty row. 您可以将groupby与自定义函数一起使用,其中添加最后一个空行。 Last use to_csv with parameter index=False for ignoring index . 最后使用带有参数index=False to_csv来忽略index

Notice: 注意:

Before writing to csv is df cast to string , because if add NaN row, all integer columns are converted to float 在写入csv之前, df强制转换为string ,因为如果添加NaN行,则所有整数列都将转换为float

def f(x):
    x.loc[-1] = pd.Series([])
    return x
df = df.astype(str).groupby(['column1','column2'], as_index=False).apply(f)

print (df)
     column1 column2 column3
0  0       A      23    blue
   1       A      23  orange
  -1     NaN     NaN     NaN
1  2       A      45  yellow
   3       A      45  yellow
   4       A      45    blue
  -1     NaN     NaN     NaN
2  5       A      60   green
   6       A      60   green
  -1     NaN     NaN     NaN
3  7       A      75    pink
  -1     NaN     NaN     NaN

#default separator is ,
df.to_csv('file.csv', index=False)
A,23,blue
A,23,orange
,,
A,45,yellow
A,45,yellow
A,45,blue
,,
A,60,green
A,60,green
,,
A,75,pink
,,

#custom separator tab
df.to_csv('file.csv', index=False, sep='\t')
column1 column2 column3
A       23      blue
A       23      orange

A       45      yellow
A       45      yellow
A       45      blue

A       60      green
A       60      green

A       75      pink

For excel use to_excel : 对于excel,使用to_excel

df.to_excel('file.xlsx', index=False)

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

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