0

source.csv file -

0,2020-12-31,20:30:00,Lorem ipsum dolor sit amet consectetur adipiscing elit. Nam hendrerit nisi sed sollicitudin pellentesque. Nunc posuere purus rhoncus pulvinar aliquam. Ut aliquet tristique nisl vitae volutpat. Nulla aliquet porttitor  mattis at dui acvenenatis. Donec a dui et dui fringilla consectetur id nec massa. Aliquam erat volutpat. Sed ut dui ut lacus dictum fermentum vel tincidunt neque. Sed sed lacinia lectus. Duis sit amet sodales felis. 
1,2021-04-01,09:05:00,test
2,2021-04-01,09:30:00,test

Python code -

import pandas as pd

df = pd.read_csv('source.csv')
df.stack().to_string('output.txt')

output.txt

0  Unnamed: 0                                                    0
   Date                                                 2020-12-31
   Time                                                   20:30:00
   Activity      Lorem ipsum dolor sit amet consectetur adipisc...
1  Unnamed: 0                                                    1
   Date                                                 2021-04-01
   Time                                                   09:05:00
   Activity                                                   test
2  Unnamed: 0                                                    2
   Date                                                 2021-04-01
   Time                                                   09:30:00
   Activity                                                   test

When I used df.stack().to_string('output.txt'), the cells with a long text got truncated. How can I avoid that, please?

If I use pd.set_option('display.max_colwidth', None), then those cells with short texts contain lots of spaces. It makes me be hardly able to read the output.

Is there any way in which I can set the display.max_colwidth only for specified columns, instead of for all of the columns?

Sherman Chen
  • 174
  • 9
  • 2
    duplicate:https://stackoverflow.com/questions/11994765/pandas-dataframe-to-string-truncating-strings-from-columns/12001086 – Sherar MDP Aug 12 '21 at 05:11
  • 2
    Does this answer your question? [pandas DataFrame.to\_string() truncating strings from columns](https://stackoverflow.com/questions/11994765/pandas-dataframe-to-string-truncating-strings-from-columns) – Cimbali Aug 12 '21 at 07:13
  • [@Sherar MDP](https://stackoverflow.com/users/13421617/sherar-mdp) and [@Cimbali](https://stackoverflow.com/users/1387346/cimbali), If I use pd.set_option('display.max_colwidth', None), then those cells with short texts contain lots of spaces, which makes me to be hardly able to read the output. – Sherman Chen Aug 12 '21 at 13:07

0 Answers0