Python 数据帧在单独的列(而不是一列)中返回值

Python 数据帧在单独的列(而不是一列)中返回值,python,pandas,dataframe,Python,Pandas,Dataframe,我正在尝试创建一个新的数据帧(df3),其中包含 另一个数据帧(df)。当我从df中选取值到df3中时,每个值 在单独的列中添加,而不是在一列中添加 以下是我所做的: df3 = pd.DataFrame(data=[df[6][3:270],index= [df[4][3:270]]) df3.columns = ['GDP in billions of chained 2009 dollars'] df3.index.rename('quarter', inplace = True)

我正在尝试创建一个新的数据帧(df3),其中包含 另一个数据帧(df)。当我从df中选取值到df3中时,每个值 在单独的列中添加,而不是在一列中添加

以下是我所做的:

df3 = pd.DataFrame(data=[df[6][3:270],index= [df[4][3:270]])

df3.columns = ['GDP in billions of chained 2009 dollars']

df3.index.rename('quarter', inplace = True)
这是我得到的错误:

长度不匹配:预期轴有267个元素,新值有1个元素

试一试


您应该使用
iloc
进行索引,使其成为
data=[df.iloc[3:270,6],index=[df.iloc[3:270,4]
。我得到了这个错误:indexer:index out of bounds index out of bounds意味着您正在尝试访问一个不可用的行。检查df中有多少行
   df3 = pd.DataFrame(df1.iloc[3:270, 6],index = [df1.iloc[3:270, 4]])