Home > Software engineering >  assigning csv file to a variable name
assigning csv file to a variable name

Time:01-09

I have a .csv file, i uses pandas to read the .csv file.

import pandas as pd
from pandas import read_csv
data=read_csv('input.csv')
print(data) 

          0             1             2             3             4             5         
0     -3.288733e-08  2.905263e-08  2.297046e-08  2.052534e-08  3.767194e-08  4.822049e-08  
1      2.345769e-07  9.462636e-08  4.331173e-08  3.137627e-08  4.680112e-08  6.067109e-08 
2     -1.386798e-07  1.637338e-08  4.077676e-08  3.339685e-08  5.020153e-08  5.871679e-08  
3     -4.234607e-08  3.555008e-08  2.563824e-08  2.320405e-08  4.008257e-08  3.901410e-08
4      3.899913e-08  5.368551e-08  3.713510e-08  2.367323e-08  3.172775e-08  4.799337e-08

My aim is to assign the file to a column name so that i can access the data in later time. For example by doing something like

new_data= df['filename']
                                                                           filename
0     -3.288733e-08,2.905263e-08,2.297046e-08,2.052534e-08,3.767194e-08,4.822049e-08  
1      2.345769e-07,9.462636e-08,4.331173e-08,3.137627e-08,4.680112e-08, 6.067109e-08 
2     -1.386798e-07,1.637338e-08,4.077676e-08,3.339685e-08,5.020153e-08,5.871679e-08  
3     -4.234607e-08,3.555008e-08,2.563824e-08,2.320405e-08,4.008257e-08,3.901410e-08
4      3.899913e-08,5.368551e-08,3.713510e-08,2.367323e-08,3.172775e-08,4.799337e-08

CodePudding user response:

I don't really like it (and I still don't completely get the point), but you could just read in your data as 1 column (by using a 'wrong' seperator) and renaming the column.

import pandas as pd

filename = 'input.csv'
df = pd.read_csv(filename, sep=';')
df.columns = [filename]

If you then wish, you could add other files by doing the same thing (with a different name for df at first) and then concatenate that with df.

A more usefull approach IMHO would be to add the dataframe to a dictionary (or a list would be possible).

import pandas as pd

filename = 'input.csv'
df = pd.read_csv(filename)
data_dict = {filename: df}
# ... Add multiple files to data_dict by repeating steps above in a loop

You can then access your data later on by calling data_dict[filename] or data_dict['input.csv']

CodePudding user response:

Looks like you want to have a separate column with source file name in your dataframe. Ckeck this question please.

  • Related