如何在python中将列值转换为列标题?

时间:2019-03-17 09:48:31

标签: python pandas

嗨,我正在尝试调整df。我想将列值转换为行标题。提前致谢。

我该如何更改?

name
0   countryCode
1   vatNumber
2   requestDate
3   valid
4   name
5   address

在python中呢?

name  countryCode  vatNumber requestDate valid name address
0

1 个答案:

答案 0 :(得分:2)

您可以按以下方式使用df.T:

df = pd.DataFrame({'name': ['countryCode', 'vatNumber','requestDate', 'valid', 'name', 'address']})

df = df.T
df

             0        1          2            3          4        5
name    countryCode vatNumber   requestDate valid      name    address

df.columns = df.values[0]
df = df.reset_index(drop = True)
df


    countryCode vatNumber   requestDate valid   name    address
0   countryCode vatNumber   requestDate valid   name    address

然后将所有非null值替换为np.nan:

df[df.notna()] = np.nan
df
countryCode vatNumber   requestDate valid   name    address
0   NaN        NaN          NaN       NaN   NaN      NaN