-1

Lets say you input: os.listdir(r'filepath')

and the output is: ['a.txt','b.txt','c.txt','d.txt','e.txt']

How could you put the file names, ['a', 'b', 'c', 'd', 'e'] into a pandas dataframe?

yatu
  • 86,083
  • 12
  • 84
  • 139
D.Kim
  • 151
  • 3
  • 10

1 Answers1

2

Use list comprehension with DataFrame contructor:

L =  ['a.txt','b.txt','c.txt','d.txt','e.txt']
df = pd.DataFrame({'col':[x.split('.')[0] for x in L]})
print (df)
  col
0   a
1   b
2   c
3   d
4   e

Thank you for suggestion @Joe Halliwell, main advantage is general solution, check this:

df = pd.DataFrame({'col': [os.path.splitext(x)[0] for x in L]})
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252