I am reading from a csv-file, and have the current values in my dataframe, where width and height is min and max value.
name width heigth apple [10, 20] [15, 18]
And now i want to split and format the columns and print them:
pd.DataFrame(df['width'].str.split(',').tolist(), columns=['width-min', 'width-max']) pd.DataFrame(df['height'].str.split(',').tolist(), columns=['height-min', 'height-max']) print(tabulate(df, headers='keys', tablefmt='psql'))
My problem is that it stills print:
name width heigth apple [10, 20] [15, 18]
Whereas I want it to print:
name min-width max-width min-height max-height apple 10 20 15 18
What am I doing wrong?
Advertisement
Answer
This code can help you ,
a = {"name":['apple'], 'width': ['[10, 20]'], 'heigth': ['[15, 18]']} import pandas as pd df = pd.DataFrame(a) df['min-width'], df['max-width'] = df['width'].apply(lambda x: x.strip('][').split(', ')[0]), df['width'].apply(lambda x: x.strip('][').split(', ')[1]) df['min-height'], df['max-height'] = df['heigth'].apply(lambda x: x.strip('][').split(', ')[0]), df['heigth'].apply(lambda x: x.strip('][').split(', ')[1]) df = df.drop(['width','heigth'],axis=1)
The df
will be,
name min-width max-width min-height max-height 0 apple 10 20 15 18
please let me know if that helps you !! Also, I am open to optimized suggestions!!