How to create a dataframe column with duplicate row value?

I read data from many files and save it in a data frame. I want the column of the data frame to indicate which file the data came to. How to create a column with the same row repeating over and over without manually entering it?

Each file I read has ~ 100 data points (but not the same number every time). When I read each of them, I agree with the file frame along the axis = 0. It should look like this.

import numpy as np
import pandas as pd
numbers = np.random.randn(5) # this data could be of any length, ~100
labels = np.array(['file01','file01','file01','file01','file01']) 
tf = pd.DataFrame()
tf['labels'] = labels
tf['numbers'] = numbers

In [8]: tf
Out[8]: 
   labels   numbers
0  file01 -0.176737
1  file01 -1.243871
2  file01  0.154886
3  file01  0.236653
4  file01 -0.195053

(Yes, I know that I can make "file01" the column heading and add each along the axis = 1, but there are reasons why I do not want to do this.)

+4
source share
1

, ! dict, DataFrame:).

import numpy as np
import pandas as pd
filename = 'file01'
numbers = np.random.randn(5) # this data could be of any length, ~100
tf = pd.DataFrame({'labels': filename , 'numbers': numbers})

In [8]: tf
Out[8]: 
   labels   numbers
0  file01 -0.176737
1  file01 -1.243871
2  file01  0.154886
3  file01  0.236653
4  file01 -0.195053
+3

Source: https://habr.com/ru/post/1629914/


All Articles