Trying to limit the number of character shown in the output of a DataFrame.
Here is an example of a DataFrame:
Abc XYZ
0 Hello How are you doing today
1 Good This is a job well done
2 Bye See you tomorrow
3 Books Read chapter 1 to 5 only
Desired output:
Abc XYZ
0 Hello How are
1 Good This is
2 Bye See you
3 Books Read chapter
This is what I tried:
pd.set_option('display.max_info_rows', 2)
pd.set_option('display.max_info_columns', 2)
pd.set_option('display.max_colwidth', 2)
max_info_rows and max_info_columns did not do anything, while max_colwidth actually expanded the characters further.
Anyway to limit the number of characters in a dataframe?
Thanks!
Try this:
df.XYZ.apply(lambda x : x.rsplit(maxsplit=len(x.split())-2)[0])
0 How are
1 This is
2 See you
3 Read chapter
just reassign it back:
df.XYZ = df.XYZ.apply(lambda x : x.rsplit(maxsplit=len(x.split())-2)[0])
print(df)
Abc XYZ
0 Hello How are
1 Good This is
2 Bye See you
3 Books Read chapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With