回答編集履歴

2

訂正……

2020/02/14 01:33

投稿

kirara0048
kirara0048

スコア1399

test CHANGED
@@ -50,37 +50,37 @@
50
50
 
51
51
 
52
52
 
53
- df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0]).dt.strftime('%Y%M').astype(float)
53
+ df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0]).dt.strftime('%Y%m%d').astype(float)
54
54
 
55
- df2.iloc[:, 0] = pd.to_datetime(df2.iloc[:, 0]).dt.strftime('%Y%M').astype(float)
55
+ df2.iloc[:, 0] = pd.to_datetime(df2.iloc[:, 0]).dt.strftime('%Y%m%d').astype(float)
56
56
 
57
- df2.iloc[:, 1] = pd.to_datetime(df2.iloc[:, 1]).dt.strftime('%Y%M').astype(float)
57
+ df2.iloc[:, 1] = pd.to_datetime(df2.iloc[:, 1]).dt.strftime('%Y%m%d').astype(float)
58
58
 
59
- df2.iloc[:, 2] = pd.to_datetime(df2.iloc[:, 2]).dt.strftime('%Y%M').astype(float)
59
+ df2.iloc[:, 2] = pd.to_datetime(df2.iloc[:, 2]).dt.strftime('%Y%m%d').astype(float)
60
60
 
61
61
 
62
62
 
63
63
  print(df)
64
64
 
65
- # birthday
65
+ # birthday
66
66
 
67
- # 12 191000.0
67
+ # 12 19100101.0
68
68
 
69
- # 9 198000.0
69
+ # 9 19800203.0
70
70
 
71
- # 8 196200.0
71
+ # 8 19620404.0
72
72
 
73
- # 10 194900.0
73
+ # 10 19490406.0
74
74
 
75
75
  print(df2)
76
76
 
77
77
  # high_school university1 university2
78
78
 
79
- # 8 197700.0 198500.0 199900.0
79
+ # 8 19770331.0 19850331.0 19990331.0
80
80
 
81
- # 9 199500.0 NaN NaN
81
+ # 9 19950331.0 NaN NaN
82
82
 
83
- # 10 195400.0 196400.0 NaN
83
+ # 10 19540331.0 19640331.0 NaN
84
84
 
85
85
  ```
86
86
 
@@ -92,17 +92,17 @@
92
92
 
93
93
  ```Python
94
94
 
95
- result = df2.sub(df['birthday'], 0) // 100
95
+ result = df2.sub(df['birthday'], 0) // 10000
96
96
 
97
97
  print(result.astype('Int64'))
98
98
 
99
99
  # high_school university1 university2
100
100
 
101
- # 8 15 23 37
101
+ # 8 14 22 36
102
102
 
103
103
  # 9 15 <NA> <NA>
104
104
 
105
- # 10 5 15 <NA>
105
+ # 10 4 14 <NA>
106
106
 
107
107
  # 12 <NA> <NA> <NA>
108
108
 
@@ -116,16 +116,16 @@
116
116
 
117
117
  ```Python
118
118
 
119
- result = df2.sub(df['birthday'].reindex(df2.index), 0) // 100
119
+ result = df2.sub(df['birthday'].reindex(df2.index), 0) // 10000
120
120
 
121
121
  print(result.astype('Int64'))
122
122
 
123
123
  # high_school university1 university2
124
124
 
125
- # 8 15 23 37
125
+ # 8 14 22 36
126
126
 
127
127
  # 9 15 <NA> <NA>
128
128
 
129
- # 10 5 15 <NA>
129
+ # 10 4 14 <NA>
130
130
 
131
131
  ```

1

追記

2020/02/14 01:33

投稿

kirara0048
kirara0048

スコア1399

test CHANGED
@@ -6,4 +6,126 @@
6
6
 
7
7
 
8
8
 
9
+ ---
10
+
11
+
12
+
13
+ 追記
14
+
15
+
16
+
17
+ ```Python
18
+
19
+ import io
20
+
21
+ import pandas as pd
22
+
23
+
24
+
25
+ text = """birthday
26
+
27
+ 12,'1910-01-01'
28
+
29
+ 9,'1980-02-03'
30
+
31
+ 8,'1962-04-04'
32
+
33
+ 10,'1949-04-06'"""
34
+
35
+ text2 = """high_school,university1,university2
36
+
37
+ 8,'1977-03-31','1985-03-31','1999-3-31'
38
+
39
+ 9,'1995-03-31',,
40
+
41
+ 10,'1954-03-31','1964-3-31',
42
+
43
+ """
44
+
45
+
46
+
47
+ df = pd.read_csv(io.StringIO(text))
48
+
49
+ df2 = pd.read_csv(io.StringIO(text2)).fillna(pd.NaT)
50
+
51
+
52
+
53
+ df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0]).dt.strftime('%Y%M').astype(float)
54
+
55
+ df2.iloc[:, 0] = pd.to_datetime(df2.iloc[:, 0]).dt.strftime('%Y%M').astype(float)
56
+
57
+ df2.iloc[:, 1] = pd.to_datetime(df2.iloc[:, 1]).dt.strftime('%Y%M').astype(float)
58
+
59
+ df2.iloc[:, 2] = pd.to_datetime(df2.iloc[:, 2]).dt.strftime('%Y%M').astype(float)
60
+
61
+
62
+
63
+ print(df)
64
+
65
+ # birthday
66
+
67
+ # 12 191000.0
68
+
69
+ # 9 198000.0
70
+
71
+ # 8 196200.0
72
+
73
+ # 10 194900.0
74
+
9
- で良いかと思われますが
75
+ print(df2)
76
+
77
+ # high_school university1 university2
78
+
79
+ # 8 197700.0 198500.0 199900.0
80
+
81
+ # 9 199500.0 NaN NaN
82
+
83
+ # 10 195400.0 196400.0 NaN
84
+
85
+ ```
86
+
87
+
88
+
89
+ として、
90
+
91
+
92
+
93
+ ```Python
94
+
95
+ result = df2.sub(df['birthday'], 0) // 100
96
+
97
+ print(result.astype('Int64'))
98
+
99
+ # high_school university1 university2
100
+
101
+ # 8 15 23 37
102
+
103
+ # 9 15 <NA> <NA>
104
+
105
+ # 10 5 15 <NA>
106
+
107
+ # 12 <NA> <NA> <NA>
108
+
109
+ ```
110
+
111
+
112
+
113
+ あるいは、
114
+
115
+
116
+
117
+ ```Python
118
+
119
+ result = df2.sub(df['birthday'].reindex(df2.index), 0) // 100
120
+
121
+ print(result.astype('Int64'))
122
+
123
+ # high_school university1 university2
124
+
125
+ # 8 15 23 37
126
+
127
+ # 9 15 <NA> <NA>
128
+
129
+ # 10 5 15 <NA>
130
+
131
+ ```