Python pandas,建立Series型別

2021-09-11 03:12:31 字數 1104 閱讀 2810

numpy只能處理數值型別的資料。pandas除了可以處理數值型別外,還可以處理非數值型別的資料(例如:字串、時間序列等)

pandas常用的資料型別:series(一維,帶標籤的陣列,對應資料庫中的一條記錄);dataframe(二維,series容器,對應資料庫中的表)

demo.py(series的建立):

import pandas as pd  # 需要pip安裝

# 建立series型別(帶標籤的陣列)

t = pd.series([13, 23, 33, 43, 53]) # 預設索引從0開始。 元素可以是數值型別,也可以是字串型別。

print(type(t)) # print(t)

'''0 13

1 23

2 33

3 43

4 53

dtype: int64

'''t2 = pd.series([13, 23, 33, 43, 53], index=list("abcde")) # 可以通過index指定索引(索引個數要與元素個數一致)

print(t2)

'''a 13

b 23

c 33

d 43

e 53

dtype: int64

'''my_dict =

t3 = pd.series(my_dict) # 可以通過字典建立series。 dtype元素型別會根據情況自動修改

print(t3)

'''age 18

name zhangsan

dtype: object

'''t4 = pd.series(my_dict, index=["age", "tel"]) # index指定的索引如果存在就正常顯示,否則對應索引的值就是nan

print(t4)

'''age 18

tel nan

dtype: object

'''

Python pandas儲存csv到S3的方法

方法一 使用stringio 當要儲存到檔案的是binary檔案時可以用bytesio,類似 from io import stringio import boto3 bucket bucket name csv buffer stringio df.to csv csv buffer s3 res...

Python Pandas的建立 查詢 修改

dataframe建立方法有很多,常用基本格式是 dataframe 構造器引數 dataframe data index coloumns in 272 df2 dataframe np.arange 16 reshape 4,4 index a b c d columns one two thr...

python pandas使用記錄

在使用numpy中array格式的矩陣時,我們通常使用如a 2 4,5 10 獲取陣列中一部分資料,但是dataframe結構的陣列就不能這麼寫,可以使用iloc方法,即index locate,另外有個相似的方法loc,這個方法是通過column名字進行資料定位的 import pandas as...