下面是一些簡便的方法來實現它,推廣到所有數據類型
考慮數據框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
默認下降零點。如果每行只有一個值,這將按需要工作。
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