我有一个跳过行列表(比如[1,5,10] - >行号),当我将其传递给 pandas read_csv 时,它忽略那些行。但是,我需要将这些跳过的行保存在不同的文本文件中。
我浏览了pandas read_csv文档和其他一些文章,但不知道如何将其保存到文本文件中。
示例:
输入文件:
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
代码:
skiprows = [1,3]
df = pandas.read_csv(file, skip_rows = skiprows)
现在输出.txt:
# Some junk to skip 1
# Some junk to skip 2
提前致谢!
答案 0 :(得分:1)
unexpected header block, expected a group, resource or an definition, e.g. '# Group <name>', '# <resource name> [<URI>]' or '# <HTTP method> <URI>
dangling message-body asset, expected a pre-formatted code block, indent every of it's line by 12 spaces or 3 tabs
答案 1 :(得分:-1)
试试这个,
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)
输入:
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
输出: 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
a b
1 2 c2
3 4 c4
6 7 c7
说明: