读入数据
import pandas as pd
reviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)
1、有些列是名字需要我们重新命名,创建一个副本,将region_1列改名region,将region_2列改名locale。
# 第一种 相当于把元组转成字典
# renamed = reviews.rename(columns=dict(region_1='region',region_2='locale'))
# 第二种
renamed = reviews.rename(columns={'region_1':'region','region_2':'locale'})
renamed.head()
2、将索引名改成"wines"
# reindexed = reviews.rename_axis('wines', axis='rows')
# reindexed.head()
# 另一种有意思的改法
redo = reviews.rename(index={1:'A',2:'B'},columns={'region_1':'region'})
redo.head()
3、读入两个(列名)相同的数据集 并进行首尾连接。
先读入两个数据集 查看数据集情况:
gaming_products = pd.read_csv("../input/things-on-reddit/top-things/top-things/reddits/g/gaming.csv")
gaming_products['subreddit'] = "r/gaming"
movie_products = pd.read_csv("../input/things-on-reddit/top-things/top-things/reddits/m/movies.csv")
movie_products['subreddit'] = "r/movies"
# gaming_products
# print()
movie_products
然后连接两个数据集:
combined_products = pd.concat([gaming_products, movie_products],axis=0,keys=['x','y'])
combined_products
'''
Attention:
axis=0 : 表示在纵轴(列)进行连接 axis=1 : 表示在横轴(行)进行连接
keys=['x','y'] : 进行表示 连接后的数据集中的数据分别是来自哪部分 具体可见下图
'''
4、再读入两个数据集,不同的是这次需要通过二者共有的相同主键(MeeetID)进行连接。
powerlifting_meets = pd.read_csv("../input/powerlifting-database/meets.csv")
powerlifting_competitors = pd.read_csv("../input/powerlifting-database/openpowerlifting.csv")
powerlifting_competitors
然后进行主键连接:
powerlifting_combined = powerlifting_meets.set_index("MeetID").join(powerlifting_competitors.set_index("MeetID"))
powerlifting_combined
注明:
以上数据来自kaggle learn