나는 이것을 시도했지만 작동하지 않았습니다.
df1['quarter'].str.contains('/^[-+](20)$/', re.IGNORECASE).groupby(df1['quarter'])
미리 감사드립니다
안녕하세요, 포럼에 오신 것을 환영합니다! 질문을 올바르게 이해했다면 매년 그룹을 구성 하시겠습니까?
물론 이미 컬럼이 있으므로 연도별로 그룹을 만들 수 있습니다.
연도 열이 없다고 가정하면 분기 열의 마지막 2자를 제외한 전체 문자열로 그룹화 할 수 있습니다 . 다음과 같이 (답을 위해 장난감 데이터 세트를 만들었습니다) :
import pandas as pd
d = {'quarter' : pd.Series(['1947q1', '1947q2', '1947q3', '1947q4','1948q1']),
'some_value' : pd.Series([1,3,2,4,5])}
df = pd.DataFrame(d)
df
이것은 우리의 장난감 데이터 프레임입니다.
quarter some_value
0 1947q1 1
1 1947q2 3
2 1947q3 2
3 1947q4 4
4 1948q1 5
이제 우리는 단순히 연도별로 그룹화하지만 마지막 두 문자를 뺍니다.
grouped = df.groupby(df.quarter.str[:-2])
for name, group in grouped:
print(name)
print(group, '\n')
산출:
1947
quarter some_value
0 1947q1 1
1 1947q2 3
2 1947q3 2
3 1947q4 4
1948
quarter some_value
4 1948q1 5
추가 설명 : 문자열에 항상 적용 할 수있는 연산을 사용했습니다. 예를 들어 다음을 확인하십시오.
s = 'Hi there, Dhruv!'
#Prints the first 2 characters of the string
print(s[:2])
#Output: "Hi"
#Prints everything after the third character
print(s[3:])
#Output: "there, Dhruv!"
#Prints the text between the 10th and the 15th character
print(s[10:15])
#Output: "Dhruv"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다