回答編集履歴
10
test
CHANGED
@@ -1,28 +1,8 @@
|
|
1
|
+
参照されている記事を読んでみて、求めている結果を理解できました。`pandas.Series` 型インスタンスを要素とする5行1列のデータフレームを作成するのですね。
|
2
|
+
|
1
3
|
```python
|
2
|
-
|
4
|
+
X = pd.DataFrame(index=df.T._series.values()).rename_axis('a').reset_index()
|
3
|
-
df = df[['a']]
|
4
|
-
print(df)
|
5
|
-
|
6
|
-
# a
|
7
|
-
# 0 [0, 43, 1, 2, 2, 21, 3, 63]
|
8
|
-
# 1 [0, 32, 1, 1, 2, 65, 3, 4]
|
9
|
-
# 2 [0, 23, 1, 5, 2, 6, 3, 4]
|
10
|
-
# 3 [0, 35, 1, 36, 2, 64, 3, 88]
|
11
|
-
# 4 [0, 12, 1, 1, 2, 3, 3, 5]
|
12
|
-
```
|
13
|
-
|
14
|
-
> 例として `df.iloc[0,:].values[0]` #0番目の行の値を取り出す
|
15
|
-
> ```python
|
16
|
-
> #0 43
|
17
|
-
> #1 2
|
18
|
-
> #2 21
|
19
|
-
> #3 63
|
20
|
-
> ```
|
21
|
-
> のようにしたいです。
|
22
|
-
|
23
|
-
そうでしたら、加工しないで元のデータフレームを使う方がよいでしょう。
|
24
|
-
```python
|
25
|
-
print(
|
5
|
+
print(X.iloc[0].values[0])
|
26
6
|
|
27
7
|
# 0 43
|
28
8
|
# 1 2
|
@@ -30,3 +10,4 @@
|
|
30
10
|
# 3 63
|
31
11
|
# Name: 0, dtype: int64
|
32
12
|
```
|
13
|
+

|
9
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
```python
|
2
|
-
df['a'] = df.apply(lambda x: list(sum(
|
2
|
+
df['a'] = df.apply(lambda x: list(sum(zip(range(df.shape[1]), x), ())), axis=1)
|
3
|
-
df = df
|
3
|
+
df = df[['a']]
|
4
4
|
print(df)
|
5
5
|
|
6
6
|
# a
|
@@ -11,21 +11,22 @@
|
|
11
11
|
# 4 [0, 12, 1, 1, 2, 3, 3, 5]
|
12
12
|
```
|
13
13
|
|
14
|
+
> 例として `df.iloc[0,:].values[0]` #0番目の行の値を取り出す
|
15
|
+
> ```python
|
16
|
+
> #0 43
|
17
|
+
> #1 2
|
18
|
+
> #2 21
|
19
|
+
> #3 63
|
14
|
-
|
20
|
+
> ```
|
21
|
+
> のようにしたいです。
|
15
22
|
|
16
|
-
|
23
|
+
そうでしたら、加工しないで元のデータフレームを使う方がよいでしょう。
|
24
|
+
```python
|
25
|
+
print(df.iloc[0])
|
17
26
|
|
18
|
-
つまり、「リストの要素をスペース区切りで連結する」ということでしょうか?
|
19
|
-
|
20
|
-
```python
|
21
|
-
df['a'] = df.apply(lambda x: ' '.join(map(str,sum(zip(range(df.shape[1]),x),()))),axis=1)
|
22
|
-
df = df[['a']]
|
23
|
-
print(df)
|
24
|
-
|
25
|
-
# a
|
26
|
-
# 0
|
27
|
+
# 0 43
|
27
|
-
# 1
|
28
|
+
# 1 2
|
29
|
+
# 2 21
|
30
|
+
# 3 63
|
28
|
-
#
|
31
|
+
# Name: 0, dtype: int64
|
29
|
-
# 3 0 35 1 36 2 64 3 88
|
30
|
-
# 4 0 12 1 1 2 3 3 5
|
31
32
|
```
|
8
test
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
```python
|
21
21
|
df['a'] = df.apply(lambda x: ' '.join(map(str,sum(zip(range(df.shape[1]),x),()))),axis=1)
|
22
|
-
df = df
|
22
|
+
df = df[['a']]
|
23
23
|
print(df)
|
24
24
|
|
25
25
|
# a
|
7
test
CHANGED
@@ -18,9 +18,8 @@
|
|
18
18
|
つまり、「リストの要素をスペース区切りで連結する」ということでしょうか?
|
19
19
|
|
20
20
|
```python
|
21
|
-
df['a'] = df.apply(lambda x:
|
21
|
+
df['a'] = df.apply(lambda x: ' '.join(map(str,sum(zip(range(df.shape[1]),x),()))),axis=1)
|
22
22
|
df = df.iloc[:,[-1]]
|
23
|
-
df['a'] = df['a'].map(lambda x: ' '.join(map(str, x)))
|
24
23
|
print(df)
|
25
24
|
|
26
25
|
# a
|
6
test
CHANGED
@@ -11,3 +11,22 @@
|
|
11
11
|
# 4 [0, 12, 1, 1, 2, 3, 3, 5]
|
12
12
|
```
|
13
13
|
|
14
|
+
**追記**
|
15
|
+
|
16
|
+
> 最終的に1列目に得られた採番ごとの値を集約したい
|
17
|
+
|
18
|
+
つまり、「リストの要素をスペース区切りで連結する」ということでしょうか?
|
19
|
+
|
20
|
+
```python
|
21
|
+
df['a'] = df.apply(lambda x: list(sum(zip(range(df.shape[1]), x), ())), axis=1)
|
22
|
+
df = df.iloc[:,[-1]]
|
23
|
+
df['a'] = df['a'].map(lambda x: ' '.join(map(str, x)))
|
24
|
+
print(df)
|
25
|
+
|
26
|
+
# a
|
27
|
+
# 0 0 43 1 2 2 21 3 63
|
28
|
+
# 1 0 32 1 1 2 65 3 4
|
29
|
+
# 2 0 23 1 5 2 6 3 4
|
30
|
+
# 3 0 35 1 36 2 64 3 88
|
31
|
+
# 4 0 12 1 1 2 3 3 5
|
32
|
+
```
|
5
test
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
```python
|
2
|
-
df['a'] = df.apply(lambda x: sum(
|
2
|
+
df['a'] = df.apply(lambda x: list(sum([*zip(range(df.shape[1]), x)], ())), axis=1)
|
3
3
|
df = df.iloc[:,[-1]]
|
4
4
|
print(df)
|
5
5
|
|
4
test
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
```python
|
2
|
-
df['a'] = df.apply(lambda x: sum(map(list, [*zip(range(len(df)), x
|
2
|
+
df['a'] = df.apply(lambda x: sum(map(list, [*zip(range(len(df)), x)]), []), axis=1)
|
3
3
|
df = df.iloc[:,[-1]]
|
4
4
|
print(df)
|
5
5
|
|
3
test
CHANGED
@@ -1,30 +1,13 @@
|
|
1
|
-
※ カラム名は適当に変更してください。
|
2
1
|
```python
|
2
|
+
df['a'] = df.apply(lambda x: sum(map(list, [*zip(range(len(df)), x.values)]), []), axis=1)
|
3
|
-
df
|
3
|
+
df = df.iloc[:,[-1]]
|
4
|
-
[dfx.insert(dfx.columns.get_loc(c), f'i{i}', [i]*len(dfx)) for i, c in enumerate(dfx.columns)]
|
5
|
-
print(df
|
4
|
+
print(df)
|
5
|
+
|
6
|
-
|
6
|
+
# a
|
7
|
+
# 0 [0, 43, 1, 2, 2, 21, 3, 63]
|
8
|
+
# 1 [0, 32, 1, 1, 2, 65, 3, 4]
|
9
|
+
# 2 [0, 23, 1, 5, 2, 6, 3, 4]
|
10
|
+
# 3 [0, 35, 1, 36, 2, 64, 3, 88]
|
11
|
+
# 4 [0, 12, 1, 1, 2, 3, 3, 5]
|
7
12
|
```
|
8
|
-
| | i0 | 0 | i1 | 1 | i2 | 2 | i3 | 3 |
|
9
|
-
|---:|-----:|----:|-----:|----:|-----:|----:|-----:|----:|
|
10
|
-
| 0 | 0 | 43 | 1 | 2 | 2 | 21 | 3 | 63 |
|
11
|
-
| 1 | 0 | 32 | 1 | 1 | 2 | 65 | 3 | 4 |
|
12
|
-
| 2 | 0 | 23 | 1 | 5 | 2 | 6 | 3 | 4 |
|
13
|
-
| 3 | 0 | 35 | 1 | 36 | 2 | 64 | 3 | 88 |
|
14
|
-
| 4 | 0 | 12 | 1 | 1 | 2 | 3 | 3 | 5 |
|
15
13
|
|
16
|
-
```pyhton
|
17
|
-
RangeIndex: 5 entries, 0 to 4
|
18
|
-
Data columns (total 8 columns):
|
19
|
-
# Column Non-Null Count Dtype
|
20
|
-
--- ------ -------------- -----
|
21
|
-
0 i0 5 non-null int64
|
22
|
-
1 0 5 non-null int64
|
23
|
-
2 i1 5 non-null int64
|
24
|
-
3 1 5 non-null int64
|
25
|
-
4 i2 5 non-null int64
|
26
|
-
5 2 5 non-null int64
|
27
|
-
6 i3 5 non-null int64
|
28
|
-
7 3 5 non-null int64
|
29
|
-
dtypes: int64(8)
|
30
|
-
```
|
2
test
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
※ カラム名は適当に変更してください。
|
2
2
|
```python
|
3
3
|
dfx = df.copy()
|
4
|
-
[dfx.insert(dfx.columns.get_loc(
|
4
|
+
[dfx.insert(dfx.columns.get_loc(c), f'i{i}', [i]*len(dfx)) for i, c in enumerate(dfx.columns)]
|
5
5
|
print(dfx)
|
6
|
+
dfx.info()
|
6
7
|
```
|
7
8
|
| | i0 | 0 | i1 | 1 | i2 | 2 | i3 | 3 |
|
8
9
|
|---:|-----:|----:|-----:|----:|-----:|----:|-----:|----:|
|
@@ -12,3 +13,18 @@
|
|
12
13
|
| 3 | 0 | 35 | 1 | 36 | 2 | 64 | 3 | 88 |
|
13
14
|
| 4 | 0 | 12 | 1 | 1 | 2 | 3 | 3 | 5 |
|
14
15
|
|
16
|
+
```pyhton
|
17
|
+
RangeIndex: 5 entries, 0 to 4
|
18
|
+
Data columns (total 8 columns):
|
19
|
+
# Column Non-Null Count Dtype
|
20
|
+
--- ------ -------------- -----
|
21
|
+
0 i0 5 non-null int64
|
22
|
+
1 0 5 non-null int64
|
23
|
+
2 i1 5 non-null int64
|
24
|
+
3 1 5 non-null int64
|
25
|
+
4 i2 5 non-null int64
|
26
|
+
5 2 5 non-null int64
|
27
|
+
6 i3 5 non-null int64
|
28
|
+
7 3 5 non-null int64
|
29
|
+
dtypes: int64(8)
|
30
|
+
```
|
1
test
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
※ カラム名は適当に変更してください。
|
2
2
|
```python
|
3
|
-
dfx = df.copy()
|
3
|
+
dfx = df.copy()
|
4
|
-
[dfx.insert(dfx.columns.get_loc(
|
4
|
+
[dfx.insert(dfx.columns.get_loc(i), f'i{i}', [i]*len(dfx)) for i in dfx.columns]
|
5
5
|
print(dfx)
|
6
6
|
```
|
7
|
-
| i0 |
|
7
|
+
| | i0 | 0 | i1 | 1 | i2 | 2 | i3 | 3 |
|
8
|
-
|-----:|----
|
8
|
+
|---:|-----:|----:|-----:|----:|-----:|----:|-----:|----:|
|
9
|
-
| 0 |
|
9
|
+
| 0 | 0 | 43 | 1 | 2 | 2 | 21 | 3 | 63 |
|
10
|
-
| 0 |
|
10
|
+
| 1 | 0 | 32 | 1 | 1 | 2 | 65 | 3 | 4 |
|
11
|
-
| 0 |
|
11
|
+
| 2 | 0 | 23 | 1 | 5 | 2 | 6 | 3 | 4 |
|
12
|
-
| 0 |
|
12
|
+
| 3 | 0 | 35 | 1 | 36 | 2 | 64 | 3 | 88 |
|
13
|
-
| 0 |
|
13
|
+
| 4 | 0 | 12 | 1 | 1 | 2 | 3 | 3 | 5 |
|
14
14
|
|