我有一个非常大的CSV文件(百万+行),我想在上面执行一些操作。问题是,有些行有一些不需要的换行符,如下所示:
New York City; Iron Man; no superpowers;
Metropolis; Superman; superpowers;
New York City;
Spider-Man;
superpowers;
Gotham; Batman; no superpowers;
New York City; Doctor Strange; superpowers;
因此,该文件有三列(location
、superhero
、superpowers
)。由于Spider Man的条目是错误的,因为它的条目之间有换行符,pandas错误地假设这是三个独立的行,即第二列和第三列中的NaNs
。你知道吗
我的想法是在使用regex导入时解决这个问题。根据this website,这个正则表达式正确地匹配所需的行,而不匹配错误的行(即Spider Man)。你知道吗
(.*[;].*[;].*)
但是,它的逆((?!(.*[;].*[;].*))
)不起作用,因为它不仅不匹配三个错误行,而且不匹配每个正常行的第三个条目。你知道吗
我的另一种方法是简单地设置列数,然后从整个文件中删除所有换行符。不过,这也不管用。你知道吗
superhero_df = pd.read_csv("superheroes.csv", sep=' *; *', skiprows=12, names=["location", "superhero", "superpower"], index_col=False, engine="python")
superhero_df = superhero_df.replace('\r\n','', regex=True)
所需的输出应如下所示:
New York City; Iron Man; no superpowers
Metropolis; Superman; superpowers;
New York City; Spider-Man; superpowers;
Gotham; Batman; no superpowers;
New York City; Doctor Strange; superpowers;
下面的正则表达式在每三个字段之后消除不需要的换行符和其他空格。它假定字段没有任何内部分号:
在使用Pandas之前,可以在循环中运行它来预处理文件。你知道吗
如果我是你,我会在一个新的文本文件中重写整个数据,只需对源文本文件进行简单的迭代,然后将结果文件加载到Pandas中,无需
re
:结果:
解读熊猫:
注意:
usecols
是唯一需要的,因为后面有分号。通过使用导入可以避免这种情况解读熊猫:
那怎么办:
替换为:
regex101
run here
相关问题 更多 >
编程相关推荐