![]() |
Question from beginners: how to combine 2 columns - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Question from beginners: how to combine 2 columns (/thread-10097.html) |
Question from beginners: how to combine 2 columns - Jack_Sparrow - May-12-2018 Hi guys, I have one table with a lot of columns: Two of them are Start Station and End Station Start Station 0 Columbus Dr & Randolph St 1 Kingsbury St & Erie St 2 Canal St & Madison St 3 Spaulding Ave & Armitage Ave 4 Clark St & Randolph St End Station \ Federal St & Polk St Orleans St & Merchandise Mart Plaza Paulina Ave & North Ave California Ave & Milwaukee Ave Financial Pl & Congress Pkwy Now I want to display the most frequent combination of start station and end station trip This is my code: import pandas as pd df = pd.read_csv('chicago.csv') # I exctract the column with start stations: x= df.iloc[:, 3] # I exctract the column with end stations: y =df.iloc[:, 4] # Put this two columns together and df2 = (x+' & '+y) #display the most frequent combination of start station and end station trip df1 = df.groupby(df2).count().sorted(df2) print (df1)However, the code doesn't work. This is what I get 'DataFrame' object has no attribute 'sorted'Actually I excpected smth like this: Columbus Dr & Randolph St & Federal St & Polk St 8 How can I fix it? Thanks J RE: Question from beginners: how to combine 2 columns - woooee - May-12-2018 There is no reason to use Pandas on a simple task like this. You can just read the csv file normally. with open('chicago.csv', 'r') as fp: for rec in fp: split_rec=rec.strip().split(",") ## you now have a list of columns and can use a ## dictionary to count the 2 combined columns print(split_rec) ## ****** I am not going to do your homework for you ***** |