Pandas commonly used data structure of Python data analysis study notes in 2020 (5)

One, Pandas basic operations

1. Pandas commonly used data structure

Common properties of Series and DataFrame:

Values: return all elements of the Series object;

index: return index;

dtype: return data type;

shape: Returns the shape of the Series data;

ndim: returns the dimension of the object;

drop: delete data;

append: increase data;

size: returns the number of objects.

import pandas as pd
import numpy as np

# 创建一个Series(序列)对象
series1 = pd.Series([2.8,3.01,8.99,8.59,5.18],index = ['a','b','c','d','e'] )     # 更改序列索引
print(series1)

# 创建一个字典
series2 = pd.Series({'中国':'北京','美国':'纽约','英国':'伦敦','日本':'东京'})
series2.drop('美国', inplace=True)      # 删除数据
print(series2)

print(series2.size)

print(series2[0:2])

import pandas as pd
import numpy as np


# 创建方法一,使用列表进行创建
list1 = [['张三',23,'男'],['李四',25,'女'],['王二',15,'男']]
df1 = pd.DataFrame(list1,columns=['姓名','年龄','性别'])      # 返回列标签
print(df1)

# 创建方法二,使用字典进行创建
df2 = pd.DataFrame({'姓名':['张三','李四','王二'],'年龄':[23,25,15],'性别':['男','女','男']})
print(df2)

# 创建方法三,使用元组进行创建
array1 = np.array(list1)
df3 = pd.DataFrame(array1,columns=['姓名','年龄','性别'],index = ['a','b','c'])
print(df3)

Operating structure display:

Guess you like

Origin blog.csdn.net/weixin_44940488/article/details/106530557
Recommended