Skip to content

Instantly share code, notes, and snippets.

@karpanGit
Last active November 3, 2019 17:22
Show Gist options
  • Save karpanGit/8f61cc5287923c5bd7c107d67bc71a65 to your computer and use it in GitHub Desktop.
Save karpanGit/8f61cc5287923c5bd7c107d67bc71a65 to your computer and use it in GitHub Desktop.
pandas: truncate string columns of pandas dataframe
# groupby over all columns that are strings and truncate them; useful in case we plan to output to excel
import pandas as pd
import numpy as np
maxChars = 2 # maximum number of characters to retain
df = pd.DataFrame({'key1': ['a','a','b','b','a'],
'key2': ['one', 'two', 'one', 'two', 'three'],
'data1': np.random.randn(5),
'data2': np.random.random(5)})
print(df)
# iterate over columns that are type object
for col in df.select_dtypes(include=[object]):
if __name__ == '__main__':
df[col] = df[col].str.slice(0, maxChars)
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment