Question

Hi it's my code for python

I almost finished my project but only one thing left which is most confusing part please help me

I have to find most occurring ending character from a to z

End a b cd ef g j.. rst u VW xy z Count Start a 2.0 0.0 10.0 7.0 49.0 1.0 2.0 2.0 0.0 0.0 ...3.0 11.0 15.0 0.0 0.0 0.0 1.0 11

For instance, output should be like this I have to find a to z.

Words starting with ‘a’ end mostly with ‘O’

Words starting with ‘b’ end mostly with ‘O’

......
No words start with ‘O’(If there's no word in the character from a to z, it should be the same with this)

....

Words starting with ‘z’ end mostly with ‘O’

For example.

from the screenshot above, e seems like the most occurring, so the answer (for a) might be 'e'

In addition,

1. I have to write this output into txt file.

2. Please get rid of decimal points from the code I don't know where they came from...

Thanks!!

-----------------------------------------------------------------------------------------

Here's my code

import numpy as np
import pandas as pd

df_input=pd.read_csv('/Users/Loveyou/Downloads/words_file2.txt',header=None)

df_input.head()

df_output=pd.DataFrame(np.zeros(676,dtype=int).reshape((26,26)),
index = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'],
columns = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'])
df_output.index.name="Start"
df_output.columns.name="End"

#Here I have mentioned first i[-1] because in DataFrame first index goes for column
#so we go for End as we mentioned the Row name
for i in df_input[0]:
df_output[i[-1]][i[0]]+=1

#axis=1 is to find colunms and add count
df_output.sum(axis=1)
df_output["Count"] = df_output.sum(axis=1)
#Because axis is default just skip 0 and find raws and add Total
df_output.sum()
df_output.loc["Total", :] = df_output.sum()

df_output.to_csv("Loveyou_project31.csv", mode='w')

df_output

I am not sure for this---->print("Words starting with ‘a’ end mostly with : {}").format(df_output['a'].value_counts())

txt file is here

https://drive.google.com/open?id=1VXtEPNBJ6ypJZ62ypeeWtzS9TjcGhBp4

0 0
Add a comment Improve this question Transcribed image text
Answer #1
############## your provided code ############################# 

import numpy as np
import pandas as pd

df_input=pd.read_csv('/Users/Loveyou/Downloads/words_file2.txt',header=None)

df_input.head()

df_output=pd.DataFrame(np.zeros(676, int).reshape((26,26)))
rows = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
columns = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
df_output.index.name="Start"
df_output.columns.name="End"
df_output.columns = columns
df_output.index = rows
  
#Here I have mentioned first i[-1] because in DataFrame first index goes for column
#so we go for End as we mentioned the Row name
for i in df_input[0]:
    df_output[i[-1]][i[0]] += 1

#axis=1 is to find colunms and add count
df_output.sum(axis=1)
df_output["Count"] = df_output.sum(axis=1)
#Because axis is default just skip 0 and find rows and add Total
df_output.sum()
df_output.loc["Total", :] = df_output.sum()

df_output.to_csv("Loveyou_project31.csv", mode='w')

############### till here #################################################################

################## additonal code as per your requirements #################################

df_temp = df_output.drop('Total')   # to remove the 'Total' row. 
df_temp = df_temp.drop('Count',axis=1)   # to remove the 'Count' column 
###### changes are not made in actual df_output. we have stored the changes in temporary dataframe df
## we have done this so as to apply 'idxmax' function which returns the index with maximum value
   max_end_list = df_temp.idxmax(axis = 1)
### code for printing which word ends mostly with which word
i=0
for char in rows:
    valid = False
    for j in rows:
        if df_output[j][char] != 0:
            valid = True
            break
    if valid == True:
        print("words starting with " + char + " ends mostly with " + max_end_list[i])
    else:
        print("No words start with " + char)
    i += 1
df_output
##########################################################################

output:

words starting with a ends mostly with e
words starting with b ends mostly with h
words starting with c ends mostly with e
words starting with d ends mostly with e
words starting with e ends mostly with e
words starting with f ends mostly with e
words starting with g ends mostly with s
words starting with h ends mostly with y
words starting with i ends mostly with e
words starting with j ends mostly with n
words starting with k ends mostly with s
words starting with l ends mostly with d
words starting with m ends mostly with e
words starting with n ends mostly with e
words starting with o ends mostly with e
words starting with p ends mostly with e
words starting with q ends mostly with c
words starting with r ends mostly with e
words starting with s ends mostly with e
words starting with t ends mostly with e
words starting with u ends mostly with e
words starting with v ends mostly with e
words starting with w ends mostly with e
No words start with x
words starting with y ends mostly with e
words starting with z ends mostly with h

a b c d e f g h i j ... r s t u v w x y z Count
a 2.0 0.0 10.0 7.0 49.0 1.0 2.0 2.0 0.0 0.0 ... 3.0 11.0 15.0 0.0 0.0 0.0 1.0 11.0 0.0 141.0
b 0.0 0.0 1.0 1.0 4.0 0.0 0.0 6.0 0.0 0.0 ... 0.0 4.0 4.0 0.0 0.0 0.0 0.0 1.0 0.0 27.0
c 1.0 0.0 1.0 5.0 38.0 0.0 4.0 1.0 0.0 0.0 ... 3.0 15.0 22.0 0.0 0.0 0.0 0.0 13.0 0.0 134.0
d 0.0 0.0 3.0 5.0 36.0 0.0 1.0 3.0 0.0 0.0 ... 4.0 6.0 12.0 0.0 0.0 1.0 0.0 6.0 0.0 85.0
e 0.0 0.0 5.0 3.0 24.0 0.0 0.0 1.0 1.0 0.0 ... 3.0 3.0 16.0 0.0 0.0 1.0 0.0 7.0 0.0 72.0
f 0.0 0.0 1.0 5.0 10.0 0.0 0.0 0.0 0.0 0.0 ... 2.0 7.0 4.0 0.0 0.0 0.0 0.0 1.0 0.0 37.0
g 0.0 0.0 0.0 2.0 3.0 0.0 0.0 1.0 0.0 0.0 ... 0.0 4.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 12.0
h 0.0 0.0 0.0 3.0 1.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 4.0 1.0 0.0 0.0 0.0 0.0 5.0 0.0 17.0
i 0.0 0.0 1.0 2.0 42.0 0.0 0.0 0.0 0.0 0.0 ... 3.0 11.0 18.0 0.0 0.0 0.0 0.0 3.0 0.0 88.0
j 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0
k 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
l 0.0 0.0 2.0 4.0 1.0 0.0 0.0 1.0 0.0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 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
y 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
z 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 ... 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0
Total 6.0 0.0 35.0 55.0 338.0 1.0 11.0 23.0 1.0 0.0 ... 25.0 130.0 136.0 0.0 0.0 3.0 5.0 80.0 0.0 996.0

27 rows × 27 columns

Add a comment
Know the answer?
Add Answer to:
Hi it's my code for python I almost finished my project but only one thing left which is most con...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT