显示新行( ) 在 PD 数据帧字符串中

问题描述 投票:0回答:2

我正在尝试创建一个数据框,其中有一列包含一串带有线条的文本。这些行由行分隔,以便于阅读。

string=['''
    hello all:. I request the following from you:


    do the laundry:
    sleep early
    wake up early
    feed the chicken

     ''']


ii=pd.DataFrame(string, columns=['requirement'])
ii

但是,当我生成数据框时,表格显示非常混乱,所有新行都由 ( ) 文本格式。

大家好:。我向您提出以下要求: 洗衣服: 早点睡 起得很早 喂鸡

如何保留 PD Dataframe 中的行以便于阅读?

python pandas string dataframe newline
2个回答
1
投票

一种快速简便的方法可能是取消换行:

print(ii.to_string().replace('\\n', '\n'))

输出:

                                                                                                                                       requirement
0  
    hello all:. I request the following from you:


    do the laundry:
    sleep early
    wake up early
    feed the chicken

     

0
投票

你可以使用:

ii['requirement'].replace('\n', '', regex=True, inplace=True)
© www.soinside.com 2019 - 2024. All rights reserved.