回答編集履歴

1

pandas版を追加

2021/03/09 07:29

投稿

ppaul
ppaul

スコア24666

test CHANGED
@@ -37,3 +37,101 @@
37
37
 
38
38
 
39
39
  エクセルの.xlsxファイルを読みたければopenpyxlを使って読むか、pandasを使って読むことになるので大幅な変更が必要です。
40
+
41
+
42
+
43
+ pandas版を作ってみました。原型は留めていないですね。
44
+
45
+
46
+
47
+ ```python
48
+
49
+ import pandas as pd
50
+
51
+ import cmath
52
+
53
+
54
+
55
+ df = pd.read_excel('complex.xlsx', header=None)
56
+
57
+ df.columns = ['mag', 'ph']
58
+
59
+ df['v'] = df.apply(lambda row: cmath.rect(row['mag'], row['ph']*cmath.pi), axis=1)
60
+
61
+ print(df)
62
+
63
+ mag = list(df['mag'])
64
+
65
+ ph = list(df['ph'])
66
+
67
+ v = list(df['v'])
68
+
69
+ print(mag)
70
+
71
+ print(ph)
72
+
73
+ print(v)
74
+
75
+ ```
76
+
77
+ 実行結果は以下です。
78
+
79
+
80
+
81
+ ```python
82
+
83
+ >>> import pandas as pd
84
+
85
+ >>> import cmath
86
+
87
+ >>>
88
+
89
+ >>> df = pd.read_excel('complex.xlsx', header=None)
90
+
91
+ >>> df.columns = ['mag', 'ph']
92
+
93
+ >>> df['v'] = df.apply(lambda row: cmath.rect(row['mag'], row['ph']*cmath.pi), axis=1)
94
+
95
+ >>> print(df)
96
+
97
+ mag ph v
98
+
99
+ 0 0.487437 0.000000 0.487437+0.000000j
100
+
101
+ 1 0.653218 134.639831 -0.277814+0.591197j
102
+
103
+ 2 0.193873 166.950760 -0.191558+0.029871j
104
+
105
+ 3 0.202087 123.128914 -0.185739-0.079625j
106
+
107
+ 4 0.583534 154.655045 -0.273125+0.515669j
108
+
109
+ 5 0.128547 56.141060 0.116130+0.055120j
110
+
111
+ 6 0.129435 -154.545288 -0.018354-0.128128j
112
+
113
+ 7 0.321489 177.046280 -0.318097-0.046578j
114
+
115
+ 8 0.029101 138.556107 -0.005103+0.028651j
116
+
117
+ 9 0.192596 -175.898743 0.182933+0.060239j
118
+
119
+ >>> mag = list(df['mag'])
120
+
121
+ >>> ph = list(df['ph'])
122
+
123
+ >>> v = list(df['v'])
124
+
125
+ >>> print(mag)
126
+
127
+ [0.487436891, 0.653218389, 0.193872913999999, 0.202087417, 0.583534479, 0.128547326, 0.12943536, 0.3214885, 0.029101485, 0.192596287]
128
+
129
+ >>> print(ph)
130
+
131
+ [0.0, 134.639831499999, 166.9507599, 123.1289139, 154.6550446, 56.14105988, -154.5452881, 177.0462799, 138.5561066, -175.898742699999]
132
+
133
+ >>> print(v)
134
+
135
+ [(0.487436891+0j), (-0.2778139468650384+0.5911968154980412j), (-0.19155787387867684+0.02987118574700291j), (-0.18573943062119574-0.07962529762736151j), (-0.27312543609590767+0.5156694525946071j), (0.11613020926096146+0.05511977429884463j), (-0.018353576473760323-0.12812750933718892j), (-0.31809650787860944-0.04657754080760128j), (-0.0051030261112949655+0.028650576847816987j), (0.18293344364875225+0.06023856705629006j)]
136
+
137
+ ```