language name
0 python John
0 C John
1 python Mark
1 Go Mark
2. in the above example, we expand rows into multiple rows by one column’s list like element; now sometimes we need to expand columns into multiple columns
let’s generate some data again
data = [ [['age','27'],'John'],[['age','30'],'Mark'] ] df = pd.DataFrame(data=data,columns=['age_info','name']) print(df)
age_info name
0 [age, 27] John
1 [age, 30] Mark
we could use to_list()
df[['attribute','value']] = df.age_info.to_list() # or df[['First','Last']] = df['age_info'].to_list() print(df)
age_info name attribute value
0 [age, 27] John age 27
1 [age, 30] Mark age 30
now same quesiton, how about the column is a string that can be split?
data = [ ['john,f','1'],['mark,y','2'] ] df = pd.DataFrame(data=data,columns=['full_name','id']) print(df)
Reprint policy:
All articles in this blog are used except for special statements
CC BY 4.0
reprint policy. If reproduced, please indicate source
robot learner
!