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?
CodePudding user response:
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!!
CodePudding user response:
You do not save the resulting df. You need to assign the modified df to a new variable or overwrite the original.
EDIT:
#create a separate df for each split operation (height and width separately)
df_1 = pd.DataFrame(df['width'].str.split(',').tolist(), columns=['width-min', 'width-max'])
df_2 = pd.DataFrame(df['height'].str.split(',').tolist(), columns=['height-min', 'height-max'])
# join the two dataframes on a common index or column
pd.merge(df_1, df_2, on=["enter common column here"])