Last active
November 3, 2019 17:22
-
-
Save karpanGit/8f61cc5287923c5bd7c107d67bc71a65 to your computer and use it in GitHub Desktop.
pandas: truncate string columns of pandas dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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