Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将数据框中的某些行转换为列_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何将数据框中的某些行转换为列

Python 如何将数据框中的某些行转换为列,python,pandas,dataframe,Python,Pandas,Dataframe,最简单的方法是什么??试试: out=df.set_index('H1').unstack().to_frame().drop_duplicates(subset=0).T 最后: out.columns=['a1','a2','a3','H3','H4'] 输出的输出: a1 a2 a3 H3 H4 0 b1 b2 b3 c1 d1 您可以使用.来记录和.join来创建新的数据帧 # note you'll need to set an index if yo

最简单的方法是什么??

试试:

out=df.set_index('H1').unstack().to_frame().drop_duplicates(subset=0).T
最后:

out.columns=['a1','a2','a3','H3','H4']
输出的输出

    a1  a2  a3  H3  H4
0   b1  b2  b3  c1  d1

您可以使用
.来记录
.join
来创建新的数据帧

# note you'll need to set an index if you only have 1 row.
df_new = pd.DataFrame(df.set_index('H1')['H2'].to_dict(),index=[0]).join(df.iloc[:,2:])


print(new_df)

   a1  a2  a3  H3  H4
0  b1  b2  b3  c1  d1

h3和h4会发生什么情况?你有一个非常好的不使用图像请看