有没有比下面的解决方案更好的方法在单个 pandas 行中导入 txt 文件?
import pandas as pd
with open(path_txt_file) as f:
text = f.read().replace("n", "")
df = pd.DataFrame([text], columns = ["text"])
.txt文件中的示例行:
Today is a beautiful day.
I will go swimming.
我尝试了pd.read_csv
但由于新行,它返回多行。
你可以用.str.cat()
[pandas-doc]连接这些行:
text = pd.read_csv(path_txt_file, sep='n', header=None)[0].str.cat()
df = pd.DataFrame([text], columns=['text'])