简体   繁体   中英

How to manipulate csv entries from horizontal to vertical

I have a csv with the following entries:

apple,orange,bannana,grape
10,5,6,4
four,seven,eight,nine
yes,yes,no,yes
3,5,7,4
two,one,six,nine
no,no,no,yes
2,4,7,8
yellow,four,eight,one
no,yes,no,no

I would like to make a new csv file with the following format and so on:

apple,10,four,yes
orange,5,seven,yes
bannana,6,seven,no
grape,4,nine,yes
apple,3,two,no
orange,5,one,no
bannana,7,six,no
grape,4,nine,yes

So after grape it starts at apple with the new values.

I have tried using pandas DataFrames but cant figure how to get the data formatted how I need it.

You could try the following in pure Python ( data.csv name of input file):

import csv
from itertools import islice

with open("data.csv", "r") as fin,\
     open("data_new.csv", "w") as fout:
    reader, writer = csv.reader(fin), csv.writer(fout)
    header = next(reader)
    length = len(header) - 1
    while (rows := list(islice(reader, length))):
        writer.writerows([first, *rest] for first, rest in zip(header, zip(*rows)))

Or with Pandas:

import pandas as pd

df = pd.read_csv("data.csv")
df = pd.concat(gdf.T for _, gdf in df.set_index(df.index % 3).groupby(df.index // 3))
df.reset_index().to_csv("data_new.csv", index=False, header=False)

Output file data_new.csv for the provided sample:

apple,10,four,yes
orange,5,seven,yes
bannana,6,eight,no
grape,4,nine,yes
apple,3,two,no
orange,5,one,no
bannana,7,six,no
grape,4,nine,yes
apple,2,yellow,no
orange,4,four,yes
bannana,7,eight,no
grape,8,one,no

You can transpose your dataframe in pandas as below.

pd.read_csv('file.csv', index_col=0, header=None).T

this question is already answered:

Can pandas read a transposed CSV?


According to your new description, the problem is completely changed. you need to split your dataframe to subsets and merge them.

# Read dataframe without header
df = pd.read_csv('your_dataframe.csv', header=None)

# Create an empty DataFrame to store transposed data
tr = pd.DataFrame()

# Create, transpose and append subsets to new DataFrame
for i in range(1,df.shape[0],3):
...     temp = pd.DataFrame()
...     temp = temp.append(df.iloc[0])
...     temp = temp.append(df.iloc[i:i+3])
...     temp = temp.transpose()
...     temp.columns = [0,1,2,3]
...     tr = d.append(temp)

Hope it works for you.

df = pd.read_csv('<source file name>')
df.T.to_csv('<destination file name>')

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