Python 熊猫:下一行不是在数据框中打印
我正在尝试写入(excel文件)/打印数据框,其中列值由下一行(\n)组成 因此,当我打印df或写入excel文件时,我看到的不是下一行,而是打印“\n”Python 熊猫:下一行不是在数据框中打印,python,pandas,Python,Pandas,我正在尝试写入(excel文件)/打印数据框,其中列值由下一行(\n)组成 因此,当我打印df或写入excel文件时,我看到的不是下一行,而是打印“\n” 如何打印下一行 我认为您需要通过使用\n拆分字符串来创建列表,然后去除开始和结束空格: 解决方案: df1 = pd.DataFrame({'lyric':textbox.splitlines()}) df1.lyric = df1.lyric.str.strip() print (df1)
如何打印下一行 我认为您需要通过使用
\n
拆分字符串来创建列表,然后去除开始和结束空格:
解决方案:
df1 = pd.DataFrame({'lyric':textbox.splitlines()})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
print (textbox.split('\n'))
['So, so you think you can tell ', 'Heaven from hell']
df1 = pd.DataFrame({'lyric':textbox.split('\n')})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
解决方案:
df1 = pd.DataFrame({'lyric':textbox.splitlines()})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
print (textbox.split('\n'))
['So, so you think you can tell ', 'Heaven from hell']
df1 = pd.DataFrame({'lyric':textbox.split('\n')})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
按列表理解:
df1 = pd.DataFrame({'lyric':[x.strip() for x in textbox.split('\n')]})
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
编辑:
我认为你需要:
我认为您需要通过使用
\n
拆分字符串来创建列表,然后去除开始和结束空格:
解决方案:
df1 = pd.DataFrame({'lyric':textbox.splitlines()})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
print (textbox.split('\n'))
['So, so you think you can tell ', 'Heaven from hell']
df1 = pd.DataFrame({'lyric':textbox.split('\n')})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
解决方案:
df1 = pd.DataFrame({'lyric':textbox.splitlines()})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
print (textbox.split('\n'))
['So, so you think you can tell ', 'Heaven from hell']
df1 = pd.DataFrame({'lyric':textbox.split('\n')})
df1.lyric = df1.lyric.str.strip()
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
按列表理解:
df1 = pd.DataFrame({'lyric':[x.strip() for x in textbox.split('\n')]})
print (df1)
lyric
0 So, so you think you can tell
1 Heaven from hell
编辑:
我认为你需要:
谢谢如果必须在同一索引=0下打印两行,则需要执行的操作。如果我使用df1.to_excel()将两行打印在同一单元格中,则无法一行接一行地打印两行,而不是一行打印。我正在尝试在“excel单元格值”的多行之间插入换行符。是否需要?仅更改
sheet.write(0,1,'Hello\nWorld',style)
为sheet.write(0,1,df1['lyric'].iloc[0],style)
和另一个解决方案我正在打印大数据帧,为此我正在使用writer=pd.ExcelWriter('pandas\u simple.xlsx',engine='xlsxwriter'),谢谢。如果必须在同一索引=0下打印两行,则需要执行的操作。如果我使用df1.to_excel()将两行打印在同一单元格中,则无法一行接一行地打印两行,而不是一行打印。我正在尝试在“excel单元格值”的多行之间插入换行符。是否需要?仅将sheet.write(0,1,'Hello\nWorld',style)
更改为sheet.write(0,1,df1['lyric'].iloc[0],style)
和另一个解决方案,我正在打印大数据帧,我正在使用writer=pd.ExcelWriter('pandas\u simple.xlsx',engine='xlsxwriter'))