I am struggling with the seemingly very simple thing.I have a pandas data frame containing very long string.
df = pd.DataFrame({'one' : ['one', 'two',
'This is very long string very long string very long string veryvery long string']})
Now when I try to print the same, I do not see the full string I rather see only part of the string.
I tried following options
print(df.iloc[2])
to_html
to_string
set_printoptions
will help me.Any ideas appreciated. Looks very simple, but not able to get it!
Is this what you meant to do ?
In [7]: x = pd.DataFrame({'one' : ['one', 'two', 'This is very long string very long string very long string veryvery long string']})
In [8]: x
Out[8]:
one
0 one
1 two
2 This is very long string very long string very...
In [9]: x['one'][2]
Out[9]: 'This is very long string very long string very long string veryvery long string'
The way I often deal with the situation you describe is to use the .to_csv()
method and write to stdout:
import sys
df.to_csv(sys.stdout)
Update: it should now be possible to just use None
instead of sys.stdout
with similar effect!
This should dump the whole dataframe, including the entirety of any strings. You can use the to_csv parameters to configure column separators, whether the index is printed, etc. It will be less pretty than rendering it properly though.
I posted this originally in answer to the somewhat-related question at Output data from all columns in a dataframe in pandas
In the newer version of pandas, use:
pd.set_option('display.max_colwidth', None)
Another, pretty simple approach is to call list function:
list(df['one'][2])
# output:
['This is very long string very long string very long string veryvery long string']
No worth to mention, that is not good to convent to list the whole columns, but for a simple line - why not
Use pd.set_option('display.max_colwidth', None)
for automatic linebreaks and multi-line cells.
This is a great resource on how to use jupyters display with pandas to the fullest.
Edited:
Used to be pd.set_option('display.max_colwidth', -1)
.
Just add the following line to your code before print.
pd.options.display.max_colwidth = 90 # set a value as your need
You can simply do the following steps for setting other additional options,
You can change the options for pandas max_columns feature as follows to display more columns
import pandas as pd
pd.options.display.max_columns = 10
(this allows 10 columns to display, you can change this as you need)
Like that you can change the number of rows as you need to display as follows to display more rows
pd.options.display.max_rows = 999
(this allows to print 999 rows at a time)
this should works fine
Please kindly refer the doc to change more options/settings for pandas
If you're using jupyter notebook, you can also print pandas dataframe as HTML table, which will print full strings.
from IPython.display import display, HTML
display(HTML(df.to_html()))
Output
one
0 one
1 two
2 This is very long string very long string very long string veryvery long string
Another easier way to print the whole string is to call values
on the dataframe.
df = pd.DataFrame({'one' : ['one', 'two',
'This is very long string very long string very long string veryvery long string']})
print(df.values)
The Output will be
[['one']
['two']
['This is very long string very long string very long string veryvery long string']]
I have created a small utility function, this works well for me
def display_text_max_col_width(df, width):
with pd.option_context('display.max_colwidth', width):
print(df)
display_text_max_col_width(train_df["Description"], 800)
I can change length of the width as per my requirement, without setting any option permanently.
Source: Stackoverflow.com