继续前面的练习,之前的文章参考:
- pandas实例-了解你的数据-Chipotle
- pandas实例-筛选与排序-Chipotle
- pandas实例-数据可视化-Chipotle
- pandas实例-了解你的数据-Occupation
- pandas实例-筛选与过滤-Euro 12
- pandas实例-筛选与过滤-Fictional Army
- pandas实例-聚合-Alcohol Consumption
- pandas实例-聚合-Occupation
- pandas实例-聚合-Regiment
- pandas实例-Apply-Student Alcohol Consumption
- pandas实例-Apply-Crime Rates
- pandas实例-Merge-MPG Cars
- pandas实例-Merge-Fictitious Names
这次的数据集,同样需要手动来构造
1. Create 3 differents Series, each of length 100, as follows:
- The first a random number from 1 to 4
- The second a random number from 1 to 3
- The third a random number from 10,000 to 30,000
s1 = pd.Series(np.random.randint(low=1 , high=5 , size=100))
s2 = pd.Series(np.random.randint(low=1 , high=4 , size=100))
s3 = pd.Series(np.random.randint(low=10000 , high=30001 , size=100))
2. Let's create a DataFrame by joinning the Series by column
把3个series拼接起来,可以使用昨天的函数:concat
df = pd.concat([s1,s2,s3] , axis=1)
3. Change the name of the columns to bedrs, bathrs, price_sqr_meter
这是要修改列名
我是直接修改了columns
df.columns=['bedrs' , 'bathrs' , 'price_sqr_meter']
这个可能不友好,所以,原作者使用的是另一个函数来修改,学习下
df.rename(columns = {0: 'bedrs', 1: 'bathrs', 2: 'price_sqr_meter'}, inplace=True)
这里使用了rename函数,参考:pandas函数-rename
4. Create a one column DataFrame with the values of the 3 Series and assign it to 'bigcolumn'
bigcolumn = pd.concat([s1 , s2 , s3])
df2 = pd.DataFrame(bigcolumn)
这里series拼接之后,还是个series,我们转成DataFrame就好了
5. Oops, it seems it is going only until index 99. Is it true?
差点儿忽略了这个问题,拼接之后,我们没有对index做设置,所以默认都是原来的index
所以呢
这里,我们重置下索引就好了
df2.reset_index(drop=True , inplace=True)
网友评论