Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据框与列名称列表匹配_Python_List_Pandas_Dataframe_Match - Fatal编程技术网

Python 将数据框与列名称列表匹配

Python 将数据框与列名称列表匹配,python,list,pandas,dataframe,match,Python,List,Pandas,Dataframe,Match,我有两个文件,第一个包含dataframe,没有列名称: 2008-03-13 15 56 0 25 2008-03-14 10 32 27 45 2008-03-16 40 8 54 35 2008-03-18 40 8 63 30 2008-03-19 45 32 81 25 以及另一个文件,其中包含以下格式的列名称列表(datetime列除外): file.read()的输出 列表(组、年龄、收入、地点) 在我的真实数据中,有更多的列

我有两个文件,第一个包含dataframe,没有列名称:

2008-03-13 15  56   0  25  
2008-03-14 10  32  27  45  
2008-03-16 40   8  54  35  
2008-03-18 40   8  63  30  
2008-03-19 45  32  81  25 
以及另一个文件,其中包含以下格式的列名称列表(datetime列除外):
file.read()的输出

列表(组、年龄、收入、地点)

在我的真实数据中,有更多的列和列名。数据帧的列按列表元素排序,即第一列对应于组,第三列对应于收入,最后一列对应于位置,等等。。 因此,我的目标是使用包含在此文件中的元素命名数据框的列。 由于明显的原因,此操作将不起作用(列表中不包含datetime列,并且列表未以python格式格式化):


我已经想象过预处理的工作,从file2的输出中删除单词List和(),并在列表的开头添加列datetime,但如果您有更优雅和快速的解决方案,请告诉我

如果列名列表以字符串的形式出现,且格式正好如此,则可以执行以下操作:

with open(file2) as f:
    list_of_columns=f.read()
list_of_columns = ['date'] + list_of_columns[5:-1].split(',')
list_of_columns = [l.strip() for l in list_of_columns] # remove leading/trailing whitespace
df=pd.read_csv(file1, sep='/t', names=list_of_columns)

如果列名列表以字符串的形式出现,且格式正好如此,则可以执行以下操作:

with open(file2) as f:
    list_of_columns=f.read()
list_of_columns = ['date'] + list_of_columns[5:-1].split(',')
list_of_columns = [l.strip() for l in list_of_columns] # remove leading/trailing whitespace
df=pd.read_csv(file1, sep='/t', names=list_of_columns)

您可以这样做:

import re

fn = r'D:\temp\.data\36972593_header.csv'
with open(fn) as f:
    data = f.read()

# it will also tolerate if `List(...) is not in the first line`
cols = ['Date'] + re.sub(r'.*List\((.*)\).*', r'\1', data, flags=re.S|re.I|re.M).replace(' ', '').split(',')

fn = r'D:\temp\.data\36972593_data.csv'
# this will also parse `Date` column as `datetime`
df=pd.read_csv(fn, sep=r'\s+', names=cols, parse_dates=[0])
结果:

In [82]: df
Out[82]:
        Date  Group  Age  Income  Location
0 2008-03-13     15   56       0        25
1 2008-03-14     10   32      27        45
2 2008-03-16     40    8      54        35
3 2008-03-18     40    8      63        30
4 2008-03-19     45   32      81        25

In [83]: df.dtypes
Out[83]:
Date        datetime64[ns]
Group                int64
Age                  int64
Income               int64
Location             int64
dtype: object

您可以这样做:

import re

fn = r'D:\temp\.data\36972593_header.csv'
with open(fn) as f:
    data = f.read()

# it will also tolerate if `List(...) is not in the first line`
cols = ['Date'] + re.sub(r'.*List\((.*)\).*', r'\1', data, flags=re.S|re.I|re.M).replace(' ', '').split(',')

fn = r'D:\temp\.data\36972593_data.csv'
# this will also parse `Date` column as `datetime`
df=pd.read_csv(fn, sep=r'\s+', names=cols, parse_dates=[0])
结果:

In [82]: df
Out[82]:
        Date  Group  Age  Income  Location
0 2008-03-13     15   56       0        25
1 2008-03-14     10   32      27        45
2 2008-03-16     40    8      54        35
3 2008-03-18     40    8      63        30
4 2008-03-19     45   32      81        25

In [83]: df.dtypes
Out[83]:
Date        datetime64[ns]
Group                int64
Age                  int64
Income               int64
Location             int64
dtype: object