Skip to main content
 首页 » 编程设计

python之如何读取其中包含数组的 .txt 文件

2024年08月12日49youxin

我想从文件中读取数据到DataFrame。但是这个文件是一种特殊的格式。包括很多这样的行:

年 = [1, 2, 3]

年龄 = [4, 5, 6]

这是特殊文件的链接:https://github.com/cuongpiger/Py-for-ML-DS-DV/blob/master/Matplotlib/Chap6_data/dulieu_year_gap_pop_life.txt

请您参考如下方法:

如果 DataFrame 需要所有值,创建 Series 字典并传递给 DataFrame 构造函数,使用 ast.literal_eval 解析列表:

import ast 
 
d = {} 
with open('dulieu_year_gap_pop_life.txt') as file: 
    splitted = file.readlines() 
    for x in splitted: 
        h, data = x.strip().split(' = ') 
        d[h] = pd.Series(ast.literal_eval(data)) 
 
df = pd.DataFrame(d) 
print (df) 
     year    pop       gdp_cap  life_exp  life_exp1950 
0    1950   2.53    974.580338    43.828         28.80 
1    1951   2.57   5937.029526    76.423         55.23 
2    1952   2.62   6223.367465    72.301         43.08 
3    1953   2.67   4797.231267    42.731         30.02 
4    1954   2.71  12779.379640    75.320         62.48 
..    ...    ...           ...       ...           ... 
146  2096  10.81           NaN       NaN           NaN 
147  2097  10.82           NaN       NaN           NaN 
148  2098  10.83           NaN       NaN           NaN 
149  2099  10.84           NaN       NaN           NaN 
150  2100  10.85           NaN       NaN           NaN 
 
[151 rows x 5 columns] 

只有 2 列使用:

df = pd.DataFrame(d, columns=['year','pop']) 
print (df) 
     year    pop 
0    1950   2.53 
1    1951   2.57 
2    1952   2.62 
3    1953   2.67 
4    1954   2.71 
..    ...    ... 
146  2096  10.81 
147  2097  10.82 
148  2098  10.83 
149  2099  10.84 
150  2100  10.85 
 
[151 rows x 2 columns]