简体   繁体   中英

Add column using pd.Series returns NaN to Pandas DataFrame for the first iteration instead of string

I am trying to fill a pandas DataFrame with data from multiple excel files. I would like to add a column ('dr_nr')to the DataFrame with the names of all the excel files. I come close, but it is not exactly as i need it. Could someone please help me?

Code

#Files are read
dir_path = os.path.dirname(os.path.realpath("pythonfile"))
onlyfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]

#filenames are added to empty dataframe
global df
df = pd.DataFrame()
data = pd.DataFrame()
count = len(onlyfiles)
dr_nr = pd.DataFrame()

for x in range(0, count):
    if onlyfiles[x].endswith("xlsx") or onlyfiles[x].endswith("xls") == True:
        data = pd.DataFrame(pe.get_array(file_name=dir_path + '\\' + onlyfiles[x]))

#this is where something goes wrong (Result 1)
        data['dr_nr']= pd.Series(str(onlyfiles[x]), index=df.index)
#i tried replacing the line above with: (Result 2)
        data['dr_nr']= pd.Series(str(onlyfiles[x]), index=None)


        df = df.append(data, ignore_index=True)

result 1:

   0  1  2       dr_nr
0   A  B  C         NaN
1   A  B  C         NaN
2   A  B  C         NaN
3   A  B  C         NaN
4   A  B  C         NaN
5   A  B  C  File2.xlsx
6   A  B  C  File2.xlsx
7   A  B  C  File2.xlsx
8   A  B  C  File2.xlsx
9   A  B  C  File2.xlsx
10  A  B  C  File3.xlsx
11  A  B  C  File3.xlsx
12  A  B  C  File3.xlsx
13  A  B  C  File3.xlsx
14  A  B  C  File3.xlsx 

result 2:

   0  1  2       dr_nr
0   A  B  C  File1.xlsx
1   A  B  C         NaN
2   A  B  C         NaN
3   A  B  C         NaN
4   A  B  C         NaN
5   A  B  C  File2.xlsx
6   A  B  C         NaN
7   A  B  C         NaN
8   A  B  C         NaN
9   A  B  C         NaN
10  A  B  C  File3.xlsx
11  A  B  C         NaN
12  A  B  C         NaN
13  A  B  C         NaN
14  A  B  C         NaN

desired result:

    0  1  2       dr_nr
0   A  B  C  File1.xlsx
1   A  B  C  File1.xlsx      
2   A  B  C  File1.xlsx       
3   A  B  C  File1.xlsx       
4   A  B  C  File1.xlsx       
5   A  B  C  File2.xlsx
6   A  B  C  File2.xlsx
7   A  B  C  File2.xlsx
8   A  B  C  File2.xlsx
9   A  B  C  File2.xlsx
10  A  B  C  File3.xlsx
11  A  B  C  File3.xlsx
12  A  B  C  File3.xlsx
13  A  B  C  File3.xlsx
14  A  B  C  File3.xlsx

Other attempts

dr_nr = dr_nr.append(onlyfiles[x], ignore_index=True)
data['dr_nr']= pd.Series(str(onlyfiles[x][0:19]), index=df.index).fillna(value='Test')
data = pd.DataFrame({'dr_nr' : len(data)}, index=pd.RangeIndex(start=0, stop=99, step=1))            
data['dr_nr']= data['dr_nr'].fillna(value='test)
data['dr_nr']= pd.Series(str(onlyfiles[x][0:19]), ignore_index=True).fillna("test")
data['dr_nr'].fillna("test")

So i can't seem to reproduce the problem. Unfortunately, the result is exactly as I would like it to be.

minimal reproducible sample

import pandas as pd
container = pd.DataFrame()
container2 = pd.DataFrame()

filenames = ['file1', 'file2', 'file3']
dataset1 = [['plastic', 5],['metal',3],['liquid',8]]
dataset2 = [['Dust', 2],['Rubber',1],['Fibres',9],['test',10]]
dataset3 = [['spam', 2],['eggs',1],['pickles',9]]

dataset4 = [dataset1, dataset2, dataset3]

df = pd.DataFrame(dataset1, columns=['material', 'quantity'])
df = pd.DataFrame(dataset2, columns=['material', 'quantity'])
df = pd.DataFrame(dataset3, columns=['material', 'quantity'])


for x in range(0,len(dataset4)):
    container = pd.DataFrame(dataset4[x])
    #container['filenames']= pd.Series(filenames[x], index = None)
    container['filenames']= pd.Series(filenames[x], index=container.index)
    container2 = container2.append(container, ignore_index=True)


print(container2)

Output

         0   1 filenames
0  plastic   5     file1
1    metal   3     file1
2   liquid   8     file1
3     Dust   2     file2
4   Rubber   1     file2
5   Fibres   9     file2
6     test  10     file2
7     spam   2     file3
8     eggs   1     file3
9  pickles   9     file3

Set files names into list, then loop over names and collect df. At end concat all collected df.

import pandas as pd

files_list = ['data1.csv', 'data2.csv']

df_list = []
for file in files_list:
    df = pd.read_csv(file)
    df['dr_nr'] = df.apply(lambda x : file, axis=1)
    df_list.append(df)

df_complete = pd.concat(df_list, ignore_index=True)

   A  B  C      dr_nr
0  1  1  1  data1.csv
1  2  2  2  data1.csv
2  3  3  3  data1.csv
3  4  4  4  data2.csv
4  5  5  5  data2.csv
5  6  6  6  data2.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM