Pandas:如何通过插入具有空值的行来更改数据帧的结构

2024-04-26 00:43:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太知道如何简单地解释我的问题 但是我需要通过插入几乎为空的行来修改DataFrame 对于软件格式兼容性问题。你知道吗

举个例子:

我需要更改这种类型的Dataframe

df = pd.DataFrame({"line1": [200, 400, 800], 
                   "line2": [400, 900, 700], 
                   "line3": [800, 700, 966], 
                   "name": ["bla", "bloo", "bloom"})
print df

   line1  line2  line3   name
0    200    400    800    bla
1    400    900    700   bloo
2    800    700    966  bloom

对这样的事情:

   line_name  line1  line2  line3
0  ID
1  name 
2  bla        200     400    800 
3  bloo       400     900    700
4  bloom      800     700    966

当然,真正的数据帧有更多的行和列。 因此,我正在寻找一种方法,它可以处理数量可变的列,而不必在行列下逐个手动添加“Blank”。你知道吗

我尝试了一些Groupby方法,制作了两个数据帧(一个只有lineIDname结构,另一个有实际的namesvalues结构,然后合并它们,但没有成功。你知道吗

任何想法都将不胜感激。你知道吗


Tags: 数据方法nameiddataframedf软件line
3条回答

不确定这正是你想要的。根据给出的示例数据帧,您可以尝试:

df = pd.DataFrame({"line1": [200, 400, 800], "line2": [400, 900, 700], "line3": [800, 700, 966], "name": ["bla", "bloo", "bloom"]})
dftemp=pd.DataFrame(columns=df.columns)
dftemp.loc[0]=(len(df.columns)-1)*['']+['ID']
dftemp.loc[1]=(len(df.columns)-1)*['']+['name']
dfnew= dftemp.append(df,ignore_index=True)
dfnew.rename(columns={'name':'line_name'}, inplace=True)
cols = dfnew.columns.tolist()
cols = cols[-1:]+cols[:-1]
dfnew = dfnew[cols]
print(dfnew)

Output:
      line_name line1 line2 line3
0        ID                  
1      name                  
2       bla   200   400   800
3      bloo   400   900   700
4     bloom   800   700   966

您可以尝试使用Setting With Enlargement解决方案:

import pandas as pd
import numpy as np

df = pd.DataFrame({"line1": [200, 400, 800],
                   "line2": [400, 900, 700], 
                   "line3": [800, 700, 966], 
                   "name": ["bla", "bloo", "bloom"]})
print df

   line1  line2  line3   name
0    200    400    800    bla
1    400    900    700   bloo
2    800    700    966  bloom
#create empty lists with last item name and ID by length of dataframe
#add to df two lines
df.loc[-1] = [np.nan for i in range(df.shape[1] - 1) ] + ['name']
df.loc[-2] = [np.nan for i in range(df.shape[1] - 1) ] + ['ID']
print df

    line1  line2  line3   name
 0    200    400    800    bla
 1    400    900    700   bloo
 2    800    700    966  bloom
-1    NaN    NaN    NaN   name
-2    NaN    NaN    NaN     ID

#sort and reset index, rename column and fill nan to empty string
df = df.sort_index().reset_index(drop=True).rename(columns={'name':'line_name'}).fillna('')
#reorder columns
df = df[['line_name','line1','line2','line3']]
print df

  line_name line1 line2 line3
0        ID                  
1      name                  
2       bla   200   400   800
3      bloo   400   900   700
4     bloom   800   700   966
df = pd.DataFrame({"line1": [200, 400, 800], "line2": [400, 900, 700], "line3": [800, 700, 966], "name": ["bla", "bloo", "bloom"]}) df.loc[-1] = [np.nan for i in range(df.shape[1] - 1) ] + ['name'] df.loc[-2] = [np.nan for i in range(df.shape[1] -1)] + ['ID'] df = df.fillna('') df=df.sort_index() df=df.reset_index() df.loc[:,['name','line1','line2','line3']]

相关问题 更多 >