嗨,我正在尝试调整df。我想将列值转换为行标题。提前致谢。
我该如何更改?
name
0 countryCode
1 vatNumber
2 requestDate
3 valid
4 name
5 address
在python中呢?
name countryCode vatNumber requestDate valid name address
0
答案 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