在这个课程里,会学到最热门的资料分析套件,pandas
import 套件
import pandas as pd
使用DataFrame产生类似table的资料结构
pd.DataFrame({'Yes': [50, 21], 'No': [131, 2]})
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
资料不一定要是数值属性
pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'], 'Sue': ['Pretty good.', 'Bland.']})
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
可以利用 index参数 来改变index的资料
pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'],
'Sue': ['Pretty good.', 'Bland.']},
index=['Product A', 'Product B'])
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
可以利用Series产生类似於list的资料结构
pd.Series([1, 2, 3, 4, 5])
0 1
1 2
2 3
3 4
4 5
dtype: int64
Series可以算是一个单column的DataFrame
因此也可以对index做调整
也因为只有单行 可以利用 name参数 改变Series的资料
pd.Series([30, 35, 40], index=['2015 Sales', '2016 Sales', '2017 Sales'], name='Product A')
2015 Sales 30
2016 Sales 35
2017 Sales 40
Name: Product A, dtype: int64
pandas也可以读取csv档案,使用.read_csv
函数
wine_reviews = pd.read_csv('./winemag-data-130k-v2.csv')
wine_reviews.shape
(129971, 14)
可以利用shape来看pandas的行列(column, row)资料有多少
wine_reviews.head()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
用head()
可以印出资料中的前5笔资料
因为.read_csv
可以取得全部的资料,因此也会将index取进来
可以使用index_col=0
来去掉index的column
wine_reviews = pd.read_csv('./winemag-data-130k-v2.csv', index_col=0)
wine_reviews.head()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
另外.to_csv
可以将dataframe转为csv档汇出
animals = pd.DataFrame({'Cows': [12, 20], 'Goats': [22, 19]}, index=['Year 1', 'Year 2'])
# Your code goes here
animals.to_csv('cows_and_goats.csv')
>>: day11 Kotlin coroutine 花生什麽事?
教材网址 https://coding104.blogspot.com/2021/06/java-e...
昨天已经有完成了在本地端新增了专案资料夹,於是我们开始新增 index.html 让我们的首页出生吧...
嗨大家~ 像昨天文章里提的,这专案会采用 Notion 来当 CMS (包含资料库),意思是部落格文...
昨天介绍了连续以及等差级数的自动填满功能,但就在今天早上我发现了一个新的功能,虽然这个功能的实用度应...
焦虑感的来袭 不得不说,在有工作的情形下,一方面要努力完成现有的工作,另一方面还得焦虑自己是否能够真...