[英]Save skip rows in pandas read csv
I have a list of skip rows ( say [1,5,10] --> row numbers) and when I passed this to pandas read_csv , it ignores those rows.我有一个跳过行的列表(比如 [1,5,10] --> 行号),当我将它传递给pandas read_csv 时,它会忽略这些行。 But, I need to save these skipped rows in a different text file.
但是,我需要将这些跳过的行保存在不同的文本文件中。
I went through pandas read_csv documentation and few other articles, but have no idea how to save this into a text file.我浏览了 pandas read_csv 文档和其他几篇文章,但不知道如何将其保存到文本文件中。
Example :例子 :
Input file :输入文件 :
a,b,c
# Some Junk to Skip 1
4,5,6
# Some junk to skip 2
9,20,9
2,3,4
5,6,7
Code :代码 :
skiprows = [1,3]
df = pandas.read_csv(file, skip_rows = skiprows)
Now output.txt :现在 output.txt :
# Some junk to skip 1
# Some junk to skip 2
Thanks in advance!提前致谢!
def write_skiprows(infile, skiprows, outfile='skiprows.csv')
maxrow = max(skiprows)
with open(infile, 'r') as f, open(outfile, 'w') as o:
for i, line in enumerate(f):
if i in skiprows:
o.write(line)
if i == maxrow:
return
try this,试试这个,
df=pd.read_csv('input.csv')
skiprows=[1,3,6]
df,df_skiprow=df.drop(skiprows),df.iloc[skiprows]
#df_skiprow.to_csv('skiprows.csv',index=False)
Input:输入:
a b
0 1 c1
1 2 c2
2 3 c3
3 4 c4
4 5 c5
5 6 c6
6 7 c7
7 8 c8
8 9 c9
9 10 c10
Output: df输出:df
a b
0 1 c1
2 3 c3
4 5 c5
5 6 c6
7 8 c8
8 9 c9
9 10 c10
df_skiprow df_skiprow
a b
1 2 c2
3 4 c4
6 7 c7
Explanation:解释:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.