Home > OS >  Creating new column with length of another column with DataFrames
Creating new column with length of another column with DataFrames

Time:03-14

I have a DataFrame containing average user ratings, languages, size and the amount of user ratings. Now I'd like to create a new column with the the amount of langauges.

Printing the languages returns:

print(df.iloc[0]['Languages'])
#DA, NL, EN, FI, FR, DE, IT, JA, KO, NB, PL, PT, RU, ZH, ES, SV, ZH

Then create a new column 'Languages Count'

for index, row in df.iterrows():
    row['Languages Count'] = len(row['Languages'].split(','))

Now looking at Languages Count they all are 1. Now I'm not entirely sure why this doesn't work. I was expecting the amount of languages for each row. So for the first one 13. The second one only has 2 languages so I'd expect 2

CodePudding user response:

You can count commas with add 1 for count number of values:

df['Languages Count'] = df['Languages'].str.count(',').add(1)

CodePudding user response:

df['Languages Count'] = df['Languages'].str.split(',').apply(len)
  • Related