문제점 : 여러 요인으로 데이터 프레임을 정렬하는 것에 대한 많은 게시물을 보았지만 동일한 것으로 여러 데이터 프레임을 정렬하는 방법에 대해서는 많지 않습니다.
목표 : 여러 데이터 프레임을 하나의 열로 정렬하여 계속해서 결합 할 수 있도록하고 싶습니다 (병합하는 더 좋은 방법이 있다는 것을 알고 있으므로 대답 할 수 있다면 도움이 될 것입니다.
내 데이터 프레임은 다음과 같습니다.
Country Meat per capita (kg/person)
40 Croatia 66.3
43 Czech Republic 83.4
44 Denmark 95.2
62 Germany 88.1
75 Iceland 86.2
111 Montenegro 57.7
117 Netherlands 85.5
124 Norway 66
147 Serbia 45.2
각 데이터 프레임은 모두 높이, 1 인당 평균 육류 소비량 및 1 인당 평균 우유 소비량과 같은 다양한 두 번째 열이있는 첫 번째 열 (인덱스를 고려하지 않음)과 동일한 국가를 가지고 있습니다.
나는 각각의 모든 데이터 프레임을 정렬하려고 시도했지만 더 적은 코드 줄로 모두 정렬하고 싶습니다.
암호:
df_tall=df_tall.sort_values(by=["Country"], ascending=True) #sort tables by country to match up and merge
df_short=df_short.sort_values(by=["Country"], ascending=True)
dfm_tall=dfm_tall.sort_values(by=["Country"], ascending=True) #sort tables by country to match up and merge
dfm_short=dfm_short.sort_values(by=["Country"], ascending=True)
dfd_tall=dfd_tall.sort_values(by=["Country"], ascending=True) #sort tables by country to match up and merge
dfd_short=dfd_short.sort_values(by=["Country"], ascending=True)
보너스 : 국가별로 모두 병합하는 방법을 알고 있다면
도와 줘서 고마워! 나는 지금 파이썬을 배우고 있습니다.
데이터 프레임을 동적으로 반복하고 정렬 할 수 있습니다.
dfs = (df_tall, df_short, dfm_tall, dfm_short, dfd_tall, dfd_short)
for df in dfs:
df.sort_values('Country', ascending=True, inplace=True)
병합하려면 :
from functools import reduce
df = reduce(lambda x, y: pd.merge(x, y, on='Country', how='outer'), dfs)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다