0

I have a dataframe as below: dataframe snippet

I want to choose the word which is most frequent in each column, and then combine all the most frequent words into a sentence, and put the sentence into the last row of the Dataframe. But I dont know how to do it, could you please help? I really appreciate it.

Paulo
  • 8,690
  • 5
  • 20
  • 34

2 Answers2

0

You shouldn't be asking this type of question on Stackoverflow. Instead, please post what you've done/tried and what error has surfaced while doing so. This way you would learn about the problem much faster instead of just asking for pure answers. Please refrain from repeating this mistake in the future. My take on this problem, however, would be something like this:

cols = df.columns
most_freq = []

for col in cols:
   word = df[col].value_counts().idxmax()
   most_freq.append(word)

word_series = pd.Series(most_freqm index=cols)
df = df.append(word_series, ignore_index=True)

Explanation: I would use value_counts().idxmax() to find the most frequent word in each column and loop through all the columns in the dataframe. I would then put those most-frequent-words into a list which I will convert into a series. This series would then be appended into the original dataframe.

Cheers! Happy coding!

Owenn
  • 638
  • 1
  • 6
  • 23
0

After getting your df, use

last_row_list = []
for col in df.columns:
    last_row_list.append(df[col].mode()[0]) #get the most frequent value

df.loc[len(df)]=last_row_list
print(df)

You could have taken help from here tho: Pandas get the most frequent values of a column

Shashwat
  • 462
  • 2
  • 9
  • Dear Shash, thank you very much for your suggestion, I successfully put the most frequent words into the last row, however, I haven't not yet answered the question that: What is the most frequent word of each column? by syntax. Could you please help? Thank you in advance! – lyo canty Aug 24 '21 at 03:23
  • I'm sorry to bother you, but my dataframe contains 10 rows and 91 columns, which makes me unable to input the values manually like this one: data = {'Name': ['Tom', 'Jack', 'Jack', 'juli'], 'marks': [99, 98, 95, 95]} So I dont know how to apply from your reference link – lyo canty Aug 24 '21 at 03:25
  • @lyocanty Is there a way how you can share the dataframe link with me, possibly a google drive link, I'll update the code here. Also you don't have to manually enter data. Just use your dataframe there, instead of df in the code. And df["column name"].mode()[0] gives us the most frequent value. – Shashwat Aug 24 '21 at 03:42
  • 1
    Oh thank you very much, here is the link for the Dataframe https://drive.google.com/file/d/1LU8Gt9AIXp7dz_r31C4FKrPBIEzpNILR/view?usp=sharing I really appreciate your help, thank you very much! Wish you best for the day! – lyo canty Aug 25 '21 at 04:11
  • @lyocanty Updated my answer! No problem! – Shashwat Aug 25 '21 at 12:21
  • I'm sorry but I can't find your updated answer... just the old one. Could you please add new comment for this? Sincere thanks – lyo canty Sep 05 '21 at 14:31