df = pd.DataFrame(
{"a" : [4, 5, 6],
"b" : [7, 8, 9],
"c" : [10, 11, 12]},
index = [1, 2, 3])
df

Untitled

df["a"]라고 컬럼을 출력하게 되면 a 컬럼에 있는 4,5,6의 값이 출력이 되는데 이것을 Series 데이터라고 부릅니다.

df["a"]

df[["a"]]

Untitled

Untitled

결과를 보시면 이렇듯 DataFrame은 2차원의 구조를 가지고 있고, Series는 1차원의 구조를 가지고 있는 것을 알 수 있습니다.

# Rows 기준 예시
df[df.Length > 7]

# Columns 기준 예시
df[['width', 'length', 'species']]
df["a"].value_counts()

Untitled

1) "a"컬럼을 기준으로 정렬하기
df["a"].sort_values()

2) DataFrame 전체에서 "a"값을 기준으로 정렬하기
df.sort_values("a")

3) 역순으로 정렬하기
df.sort_values("a", ascending=False)

4) "c"컬럼 drop 하기
df = df.drop(["c"], axis=1)
df

Untitled

Untitled

Untitled

Untitled

1) "a" 컬럼값을 Groupby하여 "b"의 컬럼값 평균값 구하기 
df.groupby(["a"])["b"].mean()

mean, sum, count, .describe() 등도 가능하다.

2) pivot_table로 평균값 구하기
pd.pivot_table(df, index="a")

이런식으로도 활용 가능
pd.pivot_table(df, index="a", values="b", aggfunc="sum")

Untitled

Untitled