0

This works if I want to print substring of one column:

print(myDf["thisCol"].str[:2])

But, if I want to substring another column and include it in the output, I'm not sure how to do this.

Contrived output of above is roughly:

0 fo
1 ba

What i want, with the second column being say "anotherCol" where the maximum length is greater than 2, is the output:

0 fo    tr
1 ba    ca

(Disclaimer: I couldn't find the answer with multiple searches. And I'm learning Pandas in a sort of sideways fashion....)

8
  • 2
    Where does the second column come from? Can you provide a minimal reproducible example with some copy-pastable data please? Commented Nov 9, 2018 at 6:54
  • Second column is the same dataFrame. This works, but is not what I want as I have to add my own spaces between the columns: print(myDf["thisCol"].str[:2]+" "+myDf["anotherCol"].str[:2]) Thanks, btw! Commented Nov 9, 2018 at 6:58
  • 1
    Fantastic, but what is myDf["thisCol"]? I would like to see the data that produces your contrived output. Also, what should "anotherCol" contain and how is it initialised? Commented Nov 9, 2018 at 6:59
  • The data is all string. I just used example data above sorry. Complete strings would just be longer in some cases. Eg. foo, bar, hi_, tree, cat, sun Commented Nov 9, 2018 at 7:09
  • I'm sure what I wan to do is simple and I am probably going about it in the wrong way.... basically I just want to be able to truncate some of the columns that I print. :) Commented Nov 9, 2018 at 7:12

2 Answers 2

1

You can provide a factor, which represents the number of spaces desired, to multiply it to a single space in the print statement

import pandas as pd

# mock data following your example, replace by your own data
col1 = ['foo', 'bar']
col2 = ['tri', 'car']
my_df = pd.DataFrame({'this_col': col1, 'another_col': col2})

# Specify your desired number of spaces between the dataframe columns print
desired_num_spaces = 10

# Print dataframes with specified columns separated by the desired number of spaces
print(my_df['this_col'].str[:2] + desired_num_spaces * ' ' + my_df['another_col'].str[:2])

gives

0    fo          tr
1    ba          ca
dtype: object

Btw: In Python the use of snake_case for variable and key names is preferred. Please do not use camelCase naming style as used in other languages like C#, Java, ...

Sign up to request clarification or add additional context in comments.

Comments

0
df=pd.DataFrame({'col1':['foo','bar'],'col2':['foo','bar'],'col3':['foo','bar']})

Select the required columns and then apply the chooper function, which will cut the string upto first two characters

def chopper(x):
    return x[:2]

print(df[['col1','col3']].applymap(chopper))

Output:

  col1 col3
0   fo   fo
1   ba   ba

Another option:

If your threshold for substring is 5 or more, you can use pandas display options

pd.set_option('max_colwidth',6)

pd.set_option('max_colwidth',6)
df=pd.DataFrame({0:['asdfzklnlkdsfnalsdfknals;dknfs','asdfs0'],1:['foo','bar'],2:['foo','bar']})
print(df)

output:

      0    1    2
0  as...  foo  foo
1  as...  bar  bar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.