1、csv文件内容(分隔符为,
)
name,age
something
tom,20
2、读取csv文件增加分隔符计数
# s = pd.read_csv(pd.compat.StringIO(text), sep=r'|', squeeze=True, header=None) s = pd.read_csv('/path/to/file.csv', sep=r'|', squeeze=True, header=None) pd.concat([ s.str.split(',', expand=True), s.str.count(',').rename('_count_sep') ], axis=1)
0 1 _count_sep
0 name age 1
1 something None 0
2 tom 20 1
另一种写法
s = pd.read_csv('/path/to/file.csv', sep=r'|', squeeze=True, header=None)
pd.concat([
s.str.split(',', expand=True).join(s.str.count(',').rename('_count_sep'))
], axis=1)
0 1 _count_sep
0 name age 1
1 something None 0
2 tom 20 1