我有一个“是/否”格式的数据框,例如
7 22
1 NaN t
25 t NaN
其中“t”代表是,我需要将其转换为 X-Y 表,因为列名是 X 坐标,索引是 Y 坐标: X Y
1 22 1
2 7 25
一个伪代码,如:if a cell = "t":
newdf.X = df.column(t)
newdf.Y = df.index(t)
最佳答案
尝试这个:
# Use np.where to get the integer location of the 't's in the dataframe
r, c = np.where(df == 't')
# Use dataframe constructor with dataframe indexes to define X, Y
df_out = pd.DataFrame({'X':df.columns[c], 'Y':df.index[r]})
df_out
输出: X Y
0 22 1
1 7 25
更新地址 @RajeshC comment :给定 df,
7 22
1 NaN t
13 NaN NaN
25 t NaN
然后:r, c = np.where(df == 't')
df_out = pd.DataFrame({'X':df.columns[c], 'Y':df.index[r]}, index=r)
df_out = df_out.reindex(range(df.shape[0]))
df_out
输出: X Y
0 22 1.0
1 NaN NaN
2 7 25.0
关于python - 将 Pandas 数据框列和索引转换为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66972735/