import pandas as pd
reviews = pd.read_csv("./winemag-data-130k-v2.csv", index_col=0)
pd.set_option('max_rows', 5)
若要更新column的name
可以用rename
function
rename(columns=({'points':'score'}))
,可將points替換成score
reviews.rename(columns=({'points':'score'}))
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
若要對index做更新,則將rename中的column更改為index
rename(index={0:'firstEntry', 1:'secondEntry'})
reviews.rename(index={0:'firstEntry', 1:'secondEntry'})
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
row和column都會有自己的attribute name
可以使用rename_axis
替換掉row和column的name
reviews.rename_axis("wines", axis='rows').rename_axis("fields", axis='columns')
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
在pandas中有3個主要合併DataFrame和Series的funciton
concat
、merge
、join
,merge
做的事和join
很像,因此先忽略這個function
concat可以將dataframe向下合併,若要忽略index,可使用ignore_index = True
parameter
canadian_youtube = pd.read_csv('./CAvideos.csv')
british_youtube = pd.read_csv('./GBvideos.csv')
pd.concat([canadian_youtube, british_youtube])
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
若要左右合併dataframe,可使用join()
函式
會根據相同的index對資料做合併
left = canadian_youtube.set_index(['title', 'trending_date'])
right = british_youtube.set_index(['title', 'trending_date'])
left.join(right, lsuffix='_CAN', rsuffix='_UK')
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}