0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

itertools,pandas

0
Posted at

備忘録

python iter_line.py
#nCr(n choose r)
#異なるn個のものから、順番を区別せずにr個を選ぶ組合せの総数

from itertools import permutations
import pandas as pd

#パターン網羅 重複なし 昇順
nums = [1,2,3,4]
all_patterns = list(permutations(nums))
df = pd.DataFrame(l)
print(df)

結果

 0  1  2  3
0   1  2  3  4
1   1  2  4  3
2   1  3  2  4
3   1  3  4  2
4   1  4  2  3
..  .. .. .. ..
19  4  1  3  2
20  4  2  1  3
21  4  2  3  1
22  4  3  1  2
23  4  3  2  1

#デカルト積 重複あり 昇順
nums = [1,2,3,4]
l2 = list(product(num, repeat=4))
df2 = pd.DataFrame(l2)
print(df2)
 0  1  2  3
0    1  1  1  1
1    1  1  1  2
2    1  1  1  3
3    1  1  1  4
4    1  1  2  1
..  .. .. .. ..
251  4  4  3  4
252  4  4  4  1
253  4  4  4  2
254  4  4  4  3
255  4  4  4  4
[256 rows x 4 columns]
# DataFrameで扱うと四則演算に便利
#加算
df1 = df[0]+df[1]
#減算
df2 = df[0]-df[1]

#最大値
df.max()
#最小値
df.min()

#文字列から数字への変更
df1 = df1.astype(int)

#数字から文字列への変更
df2 = df2.astype(str)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?