简体   繁体   中英

divide the row into two rows after several columns

I have CSV file and I try to split my row into many rows if it contains more than 4 columns

Example:-

enter image description here

Expected Output:

enter image description here

So there are way to do that in pandas or python

Sorry if this is a simple question

When there are two columns with the same name in CSV file, the pandas dataframe automatically appends an integer value to the duplicate column name

for example:

This CSV file:

在此处输入图像描述

Will become this:

df = pd.read_csv("Book1.csv")
df

在此处输入图像描述

Now to solve your question, lets consider the above dataframe as the input dataframe. Try this :

cols = df.columns.tolist()
cols.remove('id')
start = 0
end = 4
new_df = []
final_cols = ['id','x1','y1','x2','y2']
while start<len(cols):
    if end>len(cols):
        end = len(cols)
    temp = cols[start:end]
    start = end
    end = end+4
    temp_df = df.loc[:,['id']+temp]
    temp_df.columns = final_cols[:1+len(temp)]

    if len(temp)<4:
        temp_df[final_cols[1+len(temp):]] = None
    
    print(temp_df)
        
    new_df.append(temp_df)

pd.concat(new_df).reset_index(drop = True)

Result:

在此处输入图像描述

You can first set the video column as index then concat your remaining every 4 columns into a new dataframe. At last, reset index to get video column back.

df.set_index('video', inplace=True)

dfs = []
for i in range(len(df.columns)//4):
    d = df.iloc[:, range(i*4,i*4+4)]
    dfs.append(d.set_axis(['x_center', 'y_center']*2, axis=1))

df_ = pd.concat(dfs).reset_index()

I think the following list comprehension should work, but it gives an positional indexing error on my machine and I don't know why

df_ = pd.concat([df.iloc[: range(i*4, i*4+4)].set_axis(['x_center', 'y_center']*2, axis=1) for i in range(len(df.columns)//4)])
print(df_)

  video   x_center   y_center   x_center   y_center
0   1_1  31.510973  22.610222  31.383655  22.488293
1   1_1  31.856295  22.830109  32.016905  22.948702
2   1_1  32.011684  22.990689  31.933356  23.004779

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM