1
columns= ['A','B','C']
df= pd.DataFrame(columns=columns)

I have a empty dataframe like above with named column header and I have files where each file has data like A 32, B 43 , C 21 and so on. I want to fill the dataframe in such a way that each file takes up one row and adds the data in each file in the respective column headers.

example-

let there be two files like-

file1    file2

A 32     A 56
B 31     B 34
C 45     C 12

then dataframe will be

  'A'    'B'  'C'
  32     31    45
  56     34    12

edit-

for root, dirs, files in os.walk(dir_M):
    for name in files:

        L = [pd.read_csv(f, index_col=[0], header=None, sep='\s+')[1] for f in files]
        df = pd.concat(L, axis=1).T

1 Answer 1

2

Use list comprehension for list of DataFrames with concat and transpose:

#add /*.* for read all files
currentdir = 'path/*.*'

L = []
#loop by files
for file in glob.glob(currentdir):
    print (file)

    s = pd.read_csv(file, sep='|', header=None)[0]
    if (s.astype(str).str.strip().str.contains('\s+').any()):
        #print (s)
        df = s.str.rsplit(n=1, expand=True)
        df.columns = ['a','b']
        L.append(df.set_index('a')['b'])


df = pd.concat(L, axis=1, keys=range(len(L))).T
Sign up to request clarification or add additional context in comments.

11 Comments

each line in the file is a space separated file....like word=line.split(" ") and then the word[0] is the column header and word[1] is the column value
I am fetching the file from a directory so I am using os.walk with the solution but the error is coming file not found
@ubuntu_noob - solution was changed.
@ubuntu_noob - is possible use glob by directory, which return list of all files?
the index column is all 1...how to make it as numbered again and fillna as 0?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.