Home > Blockchain >  Python transpose/swap dataframe column and row in csv file
Python transpose/swap dataframe column and row in csv file

Time:11-02

I would like transpose/swap all the columns into the row of my csv files.

I tried this code:

import pandas as pd
import glob
import numpy as np

path =r'/Users/orochimaru/Documents/ParisSportif/foo/merge.csv'
stockstats_data = pd.DataFrame.transpose(path)
list_ = []

for file_ in path:
    df = pd.read_csv(file_)
    stockstats_data = pd.concat((df, stockstats_data), axis=1)

    stockstats_data.to_csv('merget.csv', index=False)

but it doesn't work. Any suggestions please? Thanks a lot guys !

CodePudding user response:

import glob
import pandas as pd

path =r'/Users/orochimaru/Documents/ParisSportif/foo/*.csv'
files = glob.glob(path)

frame = []
for file in files:
    frame.append(pd.read_csv(file))

df = pd.concat(frame)
df.T.to_csv('merget.csv', index=False)

CodePudding user response:

Thanks for your answer but it's dont work, it's say :

Traceback (most recent call last):
  File "/Users/orochimaru/Documents/ParisSportif/foot/transpose.py", line 11, in <module>
    df = pd.concat(frame)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/util/_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 347, in concat
    op = _Concatenator(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 404, in __init__
    raise ValueError("No objects to concatenate")
ValueError: No objects to concatenate

  • Related