Skip to content Skip to sidebar Skip to footer

Pandas: Convert Type Of Column

I have a dataframe with column category 0 [Рубрики/Hi-Tech/Интернет/Универсальное/ ] 1 [/Руб�

Solution 1:

I think you can use indexing with str:

df.category = df.category.str[0]  

Sample:

df = pd.DataFrame({'category': [['aw','be'],[],['tr','yt','uy'],['tre']]})
print (df)
       category
0      [aw, be]
1            []
2  [tr, yt, uy]
3         [tre]

df.category = df.category.str[0]  
print (df)
  category
0       aw
1      NaN
2       tr
3      tre

If need replace NaN with empty string:

df.category = df.category.str[0].fillna('')
print (df)
  category
0       aw
1         
2       tr
3      tre

Post a Comment for "Pandas: Convert Type Of Column"