4 Fri
ํ์
์ค๋ฌด์์๊ฒ ๋ฐฐ์ฐ๋ Kaggle ๋จธ์ ๋ฌ๋ ์
๋ฌธ
๊ณ ๊ธ pandas ํจ์๋ค - concat, groupby, query
concat
index๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฌ๋ฌ ๊ฐ์ dataframe์ ๋ณํฉํ๋ค
index๊ฐ ๋ค๋ฅผ ๊ฒฝ์ฐ
df.rename()
์ ์ฌ์ฉํด ์ธ๋ฑ์ค๋ ์ปฌ๋ผ์ ๋ณ๊ฒฝํ ์ ์๋ค๋ ๋ฐ์ดํฐ ํ๋ ์์ ์๋ก ์๋ ๋ฐ์ดํฐ๋ Nan ์ผ๋ก ํํ๋๋ค
axis = 0
: ๊ธฐ๋ณธ๊ฐ์ด๋ฉฐ, ํ ๋ฐฉํฅ โ ์ผ๋ก ๋ณํฉํ๋คaxis = 1
: ์ด ๋ฐฉํฅ โ ์ผ๋ก ๋ณํฉํ๋ค
groupby(by=์ง๊ณ๋์, axis=์ง๊ณ์ถ)
์ธ๋ฑ์ค๋ ์ปฌ๋ผ์ ๊ฐ ๋ณ๋ก ๊ทธ๋ฃนํํ ์ ์์ผ๋ ๋ณดํต ์ธ๋ฑ์ค๋ก ๊ทธ๋ฃนํํ์ฌ Aggregation(์ง๊ณ)ํ๋ค.
axis
๋ ์๋ต ๊ฐ๋ฅํ๋ฉฐ ๊ธฐ๋ณธ ๊ฐ์ ํ ๋ฐฉํฅ ์ง๊ณ์ด๋ค.์ง๊ณ ์ดํ
sum
,max
,count
,mean
๋ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
query(์์ฑํ ์ฟผ๋ฆฌ๋ฌธ)
dataframe์ ์ฟผ๋ฆฌ๋ฌธ์ผ๋ก ํํฐ๋งํ๋ค.
df.query('column == 5')
๋๋df.query('column == "์ด์ก"')
์ฒ๋ผ ์ธ ์ ์๋ค.์ซ์ ๊ฐ์ ๊ฒฝ์ฐ๋ ๊ทธ๋ฅ ์ฐ๋ฉฐ ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ๋ ํฐ ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ๋ค.
์์ ๋ฐ์ดํ ์
and
๋or
๋ฅผ ์ฌ์ฉํด์ ๋ฌถ์ ์ ์๋ค.
๊ณ ๊ธ pandas ํจ์ ์ค์ต
<์ฐ์ต๋ฌธ์ >
my_concat_df์์ ์ ์ข ์ด '์์๋ฃ ๋ฐ ๋ด๋ฐฐ'์ธ ๋ ์ฝ๋๋ฅผ ์ถ๋ ฅํด๋ณด์ธ์
๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ plotting ๋๊ตฌ - matplotlib, seaborn
ํ๋ค์ค์๋ matplotlib์ผ๋ก ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ฅ์ด ๋ด์ฅ๋์ด ์๋ค.
Series.plot()
DataFrame.plot()
์ฃผ์ Plot ์ต์
figsize : ๊ทธ๋ํ ํฌ๊ธฐ
kind : ๊ทธ๋ํ ์ข ๋ฅ
ex) scatter, bar
title : ์ ๋ชฉ
xlabel : x์ถ ์ ๋ชฉ
ylabel : y์ถ ์ ๋ชฉ
legend : ๋ฒ๋ก
grid : ๊ฒฉ์
grid=True
: ๊ทธ๋ํ ๋ค์ ๋ฐฑ์ง๊ฐ ์๋ ๊ฒฉ์๊ฐ ์๊ธด๋ค
rot : ๋๊ธ ํ์
subplots : ๋ณต์ ๊ทธ๋ํ
subplots=True
: ์๋๋ ํ ๊ทธ๋ํ์์ ์ฌ๋ฌ ์ ์ด ๊ทธ์ด์ง๊ฒ ๋๋๋ฐ, subplots์ด True๊ฐ ๋๋ฉด ๊ฐ์์ ๊ทธ๋ํ์ ํ ์ ์ด ๊ทธ์ด์ง๊ฒ ๋๋ค
seaborn์ผ๋ก ๋ฐ์ดํฐ ๋ถํฌ ์ดํด๋ณด๊ธฐ
๋ฐ์ดํฐ์ ํต๊ณ์ ์ธ ๋ถ๋ถ์ ์ดํด๋ณผ ๋ matplotlib์ ๋นํด ์ฝ๊ณ ๊ฐํธํ๋ค.
๋ณดํต
sns
์ด๋ผ๋ ์ฝ์ด๋ก ์ฌ์ฉํ๋คimport seaborn as sns
sns.load_dataset('tips')
๋ด์ฅ ๋ฐ์ดํฐ์ ๋ถ๋ฌ์ค๊ธฐ
sns.countplot
๋ฐ์ดํฐ ์๋ฅผ ์ธ๋ countplot
sns.boxplot
4๋ถ์ ๋ํ
sns.scatterplot
๋ ๋ณ์ ๊ด๊ณ๋ฅผ ์ ์ฐ์ด ๊ทธ๋ฆฌ๊ธฐ
sns.jointplot
๋ ๋ณ์ ๊ด๊ณ๋ฅผ ์ ๊ณผ ๋ถํฌ๋ก ๋ณด๊ธฐ
hue
์ธ์ ์ฌ์ฉ๊ทธ๋ํ ๋ด์์ ๋ ๋ค๋ฅธ group์ผ๋ก ๋๋ ์ ์๋ค.
sns.violinplot
๋ฐ์ด์ฌ๋ฆฐ ๋ชจ์์ ๊ทธ๋ํ
sns.distplot
๋ฐ์ดํฐ์ ๋ถํฌ๊ฐ ๊ทธ๋ ค์ง ๋ง๋ ๊ทธ๋ํ
kde=True
: ์ ๊ทธ๋ํ๊ฐ ์ถ๊ฐ๋๋คrug=True
: ๋ฐ์ดํฐ์ ๋ถํฌ๋๊ฐ ์ถ๊ฐ๋๋ค.
matplotlib, seaborn ์ค์ต
<์ฐ์ต๋ฌธ์ >
trafic_data์์ '์ค๊ณ ๋๋ผ'์ ์ผ์๋ณ Bar ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ค๋ณด์ธ์
trafic_data.loc[:,'์ค๊ณ ๋๋ผ'].plot(kind='bar')
<์ฐ์ต๋ฌธ์ >
์ฑ๋ณ ๋ณ๋ก, ํ์ ์ค ํ์๋ฅผ ์ฐจํธ๋ก ๊ทธ๋ ค๋ณด์ธ์
sns.countplot(data=tips, x='sex');
Last updated
Was this helpful?