2017-09-29 4 views
-1
fake = {'EmployeeID' : [0,1,2,3,4,5,6,7,8,9], 
      'State' : ['a','b','c','d','e','f','g','h','i','j'], 
      'Email' : ['a','b','c','d','e','f','g','h','i','j'] 
       } 
fake_df = pd.DataFrame(fake) 

상태에있는 직원의 모든 전자 메일 주소 문자열을 반환하는 함수를 정의하려고합니다. 전자 메일 주소는 지정된 구분 기호로 구분해야합니다. 나는 ";"을 사용할 것이라고 생각합니다.데이터 프레임에서 시리즈를 가져 오는 방법은 무엇입니까?

인수 : - DataFrame - 구분자 (;)

내가 for 루프를 사용해야합니까? 솔직히 말해서, 난 이것에 시작하는 방법을 모르는 .. 코딩을 수행 한 후

==== EDITION은

, 내가

emails = getEmailListByState(fake_df, ", ") 
for state in sorted(emails.index): 
    print "%15s: %s" % (state, emails[state]) 

를 실행해야하고 뭔가를 얻어야한다 내가 제대로 문제를 이해하면 내 출력으로

a: a 
b: b 
c: c,d 
d: e 
e: f,g 

+0

을 가지고 싶다면 DataFrame [indexing] (https://pandas.pydata.org/pandas-docs/stable/indexing.html) 및 [가입] (https://docs.python.org/2/library/stdtypes.html# str.join) 문자열의 반복 가능 – bunji

+0

BTw, 예상 한 결과는 무엇입니까 – Wen

+0

나는 내 게시물 –

답변

1

당신은 GROU를 찾고 있습니다 PBY 상태, 즉 즉 상태에 따라 이메일 가입에 가입 이메일을 얻을 적용

fake = {'EmployeeID' : [0,1,2,3,4,5,6,7,8,9], 
     'State' : ['NZ','NZ','NY','NY','ST','ST','YK','YK','YK','YK'], 
     'Email' : ['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]'] 
      } 
fake_df = pd.DataFrame(fake) 

ndf = fake_df.groupby('State')['Email'].apply(', '.join) 

출력 :

 
State 
NY       [email protected], [email protected] 
NZ       [email protected], [email protected] 
ST       [email protected], [email protected] 
YK [email protected], [email protected], [email protected], [email protected] 
Name: Email, dtype: object 

당신은 방법에 다음

def getEmailListByState(df,delim): 
    return df.groupby('State')['Email'].apply(delim.join) 

emails = getEmailListByState(fake_df, ", ") 
for state in sorted(emails.index): 
    print("%15s: %s" % (state, emails[state]) 
관련 문제