-1

I have multiple untidy data frames with only one column (more than 500 csv files)and all of them are in a same directory

all of them in one column and with out row numbers untidy csv

how can I sort rows and columns and merge them in one csv file? I want to tidy my csv files and merge them like this enter image description here

merged csv

tara1368
  • 1
  • 1
  • 1
    You must provide your input as **text**, images of data are not acceptable – mozway Apr 29 '23 at 11:09
  • can you please provide more information on the input DataFrame? I don't see the values that are in the "merged csv" in the "untidy csv" - can you please provide in table format? – hlin03 Apr 29 '23 at 13:25
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Apr 29 '23 at 20:07

1 Answers1

0

You need to split your string, then pivot:

df = pd.DataFrame({'col': ['NAME nokia1000', 'MAKE nokia', 'PRICE 509', 'COLOR blue']})

out = (df['col']
    .str.split(n=1, expand=True)
    .assign(index=lambda d: d[0].eq('NAME').cumsum().sub(1))
    .pivot(index='index', columns=0, values=1)
    .rename_axis(index=None, columns=None)
)

Output:

  COLOR   MAKE       NAME PRICE
0  blue  nokia  nokia1000   509
mozway
  • 194,879
  • 13
  • 39
  • 75