1

I am using the counter function to count every word of the description of 20000 products and see how many times this word repeats like 'pipette' repeats 1282 times.To do this i have split a column A into many columns P,Q,R,S,T,U & V

df["P"] = df["A"].str.split(n=10).str[0]
df["Q"] = df["A"].str.split(n=10).str[1]
df["R"] = df["A"].str.split(n=10).str[2]
df["S"] = df["A"].str.split(n=10).str[3]
df["T"] = df["A"].str.split(n=10).str[4]
df["U"] = df["A"].str.split(n=10).str[5]
df["V"] = df["A"].str.split(n=10).str[6]

This shows the splitted products

And the i am individually counting all of the columns and then add them to get the total number of words.

d = Counter(df['P'])
e = Counter(df['Q'])
f = Counter(df['R'])
g = Counter(df['S'])
h = Counter(df['T'])
i = Counter(df['U'])
j = Counter(df['V'])
m = d+e+f+g+h+i+j 
print(m)

This is the image of the output i obtained on using counter.

Now i want to transfer the output into a excel sheet with the Keys in one column and the Values in another.

Am i using the right method to do so? If yes how shall i push them into different columns.

Note: Length of each key is different

Also i wanna make all the items of column 'A' into lower case so that the counter does not repeat the items. How shall I go about it ?

Tg-10
  • 13
  • 2

1 Answers1

0

I've been learning python for just a couple of months but I'll give it a shot. I'm sure there are some better ways to perform that same action. Maybe we both can learn something from this question. Let me know how this turns out. GoodLuck

import pandas as pd
num = len(m.keys())  
df = pd.DataFrame(columns=['Key', 'Value']
for i,j,k in zip(range(num), m.keys(), m.values()):
    df.loc[i] = [j, k]

df.to_csv('Your_Project.csv')  
  • i had to create a dict named d just to be sure this works, jut replace d with m and this should work – Moha Musleh Sep 18 '18 at 11:08
  • I also feel like pandas could offer you an easier time dealing with such application like finding the number of word repetition, You should diffidently explore pandas – Moha Musleh Sep 18 '18 at 11:11
  • Hey i have two sets of keys and values when i execute them together oly on set of keys and values comes in my excel sheet.Wat shall i do? should it be done separately? – Tg-10 Sep 18 '18 at 11:33
  • what do you mean by two sets of keys and values exactly? – Moha Musleh Sep 18 '18 at 11:46
  • like 'm' i have one more counter called 'single' and i want to place both their keys and values in the same excel sheet – Tg-10 Sep 18 '18 at 11:56
  • you have to concatenate the two data frames then apply the same for loop on the concatenated data frame. https://pandas.pydata.org/pandas-docs/stable/merging.html – Moha Musleh Sep 18 '18 at 12:22
  • Hey thank you that was perfect. Now how shall i but the values and their respective keys in the descending order ? – Tg-10 Sep 20 '18 at 10:23
  • I'm glad that I could help :) – Moha Musleh Sep 20 '18 at 10:24
  • Hey what about the descending order ? any idea? – Tg-10 Sep 20 '18 at 10:45
  • Use the sort method of pandas https://stackoverflow.com/questions/24988873/python-sort-descending-dataframe-with-pandas – Moha Musleh Sep 20 '18 at 18:23