python——0-1矩阵、prod()连乘函数、字符串和list转换

(1)DataFrame格式的数据得到0-1矩阵
DataFrame格式的数据如下:

data: 
    0  1    2    3
0  a  c    e  NaN
1  b  d  NaN  NaN
2  b  c  NaN  NaN
3  a  b    c    d
4  a  b  NaN  NaN
5  b  c  NaN  NaN
6  a  b  NaN  NaN
7  a  b    c    e
8  a  b    c  NaN
9  a  c    e  NaN

转化代码:

import pandas as pd

input_path = 'F:/DataMining/chapter5/menu_orders.xls'
data = pd.read_excel(input_path, header=None)

# 匿名函数ct的作用是取出x中不是NAN的元素作为索引列,然后对应的值都为1。
# pd.Series()要求值的个数要么和索引的个数相等,要么只给定一个值然后按照索引的个数复制
ct = lambda x: pd.Series(1, index=x[pd.notna(x)])
# 多维矩阵相当于是列表嵌套列表,所以map(ct, data.as_matrix())的作用是对列表中的每一个列表都执行匿名函数ct,
# 也就是对矩阵的每一行(因为数组的每一行是一个列表)都执行匿名函数ct。
b = map(ct, data.as_matrix())
# python3中map()返回的是迭代器,可以通过list(b)查看结果
# print('list(b): \n', list(b))执行完毕后记得注释掉该语句,否则会影响后面语句的执行
print('list(b): \n', list(b))
# list(b)中的每一项都是一个Series,但是每个Series的索引个数是不相等的,所以
# 成在转换DataFrame格式的时候将索引放到了列名上
data_01matrix = pd.DataFrame(list(b)).fillna(0)  # 实现矩阵转换,NAN用0填充
print('data_01matrix: \n', data_01matrix)
print(u'\n转换完毕。')

结果如下:

list(b): 
 [a    1
c    1
e    1
dtype: int64, 
b    1
d    1
dtype: int64, 
b    1
c    1
dtype: int64, 
a    1
b    1
c    1
d    1
dtype: int64, 
a    1
b    1
dtype: int64, 
b    1
c    1
dtype: int64, 
a    1
b    1
dtype: int64, 
a    1
b    1
c    1
e    1
dtype: int64, 
a    1
b    1
c    1
dtype: int64, 
a    1
c    1
e    1
dtype: int64]

data_01matrix: 
      a    c    e    b    d
0  1.0  1.0  1.0  0.0  0.0
1  0.0  0.0  0.0  1.0  1.0
2  0.0  1.0  0.0  1.0  0.0
3  1.0  1.0  0.0  1.0  1.0
4  1.0  0.0  0.0  1.0  0.0
5  0.0  1.0  0.0  1.0  0.0
6  1.0  0.0  0.0  1.0  0.0
7  1.0  1.0  1.0  1.0  0.0
8  1.0  1.0  0.0  1.0  0.0
9  1.0  1.0  1.0  0.0  0.0

转换完毕。

(2)prod()连乘函数
Series不能实现numeric_only。
例1:Series不含字符

# prod()连乘函数
import pandas as pd

series1 = pd.Series([1, 2, 3, 4])
print('series: \n', series1)
print('series_prod: \n', series1.prod())
结果为:
series1: 
 0    1
1    2
2    3
3    4
dtype: int64
series1_prod: 
 24

例2:Series含字符

series2 = pd.Series([1, 'a', 3, 4])
print('series: \n', series2)
print('series_prod: \n', series2.prod())
# series中只允许含有一个字符否则调用prod()会报错
结果为:
series2: 
 0    1
1    a
2    3
3    4
dtype: object
series2_prod: 
 aaaaaaaaaaaa

DataFrame可以选择按行或按列连乘,可以实现numeric_only。

例3:DataFrame不含字符

dataframe1 = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]])
print('dataframe1: \n', dataframe1)
print('dataframe1_prod0: \n', dataframe1.prod(axis=0))
print('dataframe1_prod1: \n', dataframe1.prod(axis=1))
结果如下:
dataframe1: 
    0  1  2  3
0  1  2  3  4
1  5  6  7  8
dataframe1_prod0: 
 0     5
1    12
2    21
3    32
dtype: int64
dataframe1_prod1: 
 0      24
1    1680
dtype: int64

例4:DataFrame含字符(numeric_only=True)

dataframe2 = pd.DataFrame([[1, 'a', 's', 4], [5, 6, 7, 8]])
print('dataframe2: \n', dataframe2)
# numeric_only=True表示只允许数字之间连乘,数字和字符相乘时必须将字符看作1(而且当且仅当axis=1时才使用)
# 当axis=0即按列连乘时,过滤掉含有字符的列
# 当axis=1即按行连乘时将字符用1代替
print('dataframe2_prod01: \n', dataframe2.prod(axis=0, numeric_only=True))
print('dataframe2_prod11: \n', dataframe2.prod(axis=1, numeric_only=True))
结果如下:
dataframe2: 
    0  1  2  3
0  1  a  s  4
1  5  6  7  8
dataframe2_prod0: 
 0     5
3    32
dtype: int64
dataframe2_prod1: 
 0     4
1    40
dtype: int64

例5:DataFrame含字符(numeric_only=False)

dataframe2 = pd.DataFrame([[1, 'a', 's', 4], [5, 6, 7, 8]])
print('dataframe2: \n', dataframe2)
# numeric_only=False表示允许字符和数字相乘,但不允许字符和字符相乘
print('dataframe2_prod02: \n', dataframe2.prod(axis=0, numeric_only=False))
print('dataframe2_prod12: \n', dataframe2.prod(axis=1, numeric_only=False))

结果如下:
dataframe2: 
    0  1  2  3
0  1  a  s  4
1  5  6  7  8

dataframe2_prod02: 
 0          5
1     aaaaaa
2    sssssss
3         32
dtype: object

dataframe2_prod12: 
Error

(3)字符串和list转换

# list转字符串
ms = '---'
list1 = ['a', 'b', 'c']
s = ms.join(list1)
print('s: \n', s)
结果为:
s: 
 a---b---c
 
# 字符串转list
list2 = s.split('---')
print('list2: \n', list2)
结果为:
list2: 
 ['a', 'b', 'c']
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值