将CSV文件赋值给一个变量名

huangapple go评论58阅读模式
英文:

assigning csv file to a variable name

问题

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

我的目标是将文件分配给一个列名,以便以后可以访问数据。例如通过执行以下操作:

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
英文:

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

答案1

得分: 2

我不太喜欢它(而且我还不完全明白其中的要点),但你可以将数据读取为1列(通过使用一个'错误'的分隔符)并重命名列。

import pandas as pd

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

如果你想的话,可以通过执行相同的操作(最初使用不同的名称替换df)来添加其他文件,然后将其与df连接起来。

我个人认为更有用的方法是将数据帧添加到字典中(或者也可以使用列表)。

import pandas as pd

filename = 'input.csv'
df = pd.read_csv(filename)
data_dict = {filename: df}
# ... 在循环中重复上述步骤以添加多个文件

然后稍后可以通过调用`data_dict[filename]``data_dict['input.csv']`来访问你的数据
英文:

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']

huangapple
  • 本文由 发表于 2023年1月9日 17:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定