すべてのデータ型に一般にそれを行うには、いくつかの便利な方法があります
は、デフォルトではデータフレームdf
v = np.empty((8, 8), dtype=object)
v.fill(None)
i = np.arange(8)
v[i, i] = [123, 432, 53, 221, 2, 3, 'hello', 12]
df = pd.DataFrame(v, list('abcdefgh'), ['Column1[%s]' % i for i in range(1, 9)])
df
Column1[1] Column1[2] Column1[3] Column1[4] Column1[5] Column1[6] Column1[7] Column1[8]
a 123 None None None None None None None
b None 432 None None None None None None
c None None 53 None None None None None
d None None None 221 None None None None
e None None None None 2 None None None
f None None None None None 3 None None
g None None None None None None hello None
h None None None None None None None 12
オプション1
stack
はヌルをドロップ。考えてみましょうこれは、行ごとに1つの値しかない場合に必要に応じて機能します。
df.stack()
a Column1[1] 123
b Column1[2] 432
c Column1[3] 53
d Column1[4] 221
e Column1[5] 2
f Column1[6] 3
g Column1[7] hello
h Column1[8] 12
dtype: object
または
df.stack().reset_index(1, drop=True)
a 123
b 432
c 53
d 221
e 2
f 3
g hello
h 12
dtype: object
オプション2
apply
とdropna
df.apply(lambda x: x.dropna()[0], 1)
a 123
b 432
c 53
d 221
e 2
f 3
g hello
h 12
dtype: object
オプション3
np.where
の組み合わせpd.DataFrame.lookup
i, j = np.where(df.notnull())
idx = df.index[i]
col = df.columns[j]
pd.Series(df.lookup(idx, col), idx)
a 123
b 432
c 53
d 221
e 2
f 3
g hello
h 12
dtype: object