pandas merge /concat/tidy

Yeju Ham
2 min readMar 9, 2021

--

  1. pandas first row header
new_header = df.iloc[0] # grab the first row for the header
df = df[1:] # take the data less the header row
df.columns = new_header # set the header row as the df header

make it to function

def mydf(myurl):
df = pd.read_csv(urlhead + myurl, thousands =',').transpose()
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
return df[-1:]
#-1로 끝에서부터 정렬해서 가장 최근것만을 보여줌. 지정안하면 모든 분기를 다 보여줌

2. concat

df = pd.concat([df1, df2, df3, df4],axis=1)

3. how to reset index and delete that moved to the first column

df= df.reset_index() #get a index to first column
df= df.drop(df.columns[0],axis=1) #drop that index above

4. drop

df =df.drop(6) #romove remove row6df= df.

5. Merge

df.merge("붙일 내용", how = "(방법)", on ="(기준 feature)")

how

  1. inner : common data
  2. outer: every data
  3. left: data coming left will be the first
  4. right: data coming right will be the first
two ways to merge 
1. df= left.merge(right, how ="left", on ="sex")
2. df= pd.merge(left,right, how="left", on ="sex")

6. condition

1. df_new = df[( (df['age'] > 0) & (df['age'] < 10))]2. condition = df[( (df['age'] > 0) & (df['age'] < 10))]
df_new = df[condition]

7. tidy

tidy shows one observation in one row.

You can use melt() function in pandas to transform wide data →tidy data

df_tidy = df.melt(id_vars ='name', value_vars =['age','income','insurance'])
df_tidy =df_tidy.rename(columns= {'A': 'REPLACED A', 'B' : 'REPLACED B'})

You can use pivot_table() function in pandas to transform tidy data →wide data

wide = tidy.pivot_table(index = "row", columns="column", values="value")

# index: unique identifier

# columns: “wide” 데이터에서 column별로 다르게 하고자 하는 값.

# values: 결과값이 들어가는 곳 (wide 데이터프레임의 내용에 들어갈 값)

--

--

Yeju Ham
Yeju Ham

Written by Yeju Ham

learner, writer, traveler, data science beginner with the whole passion

No responses yet