我试图根据一个键('r_id')合并多个文件,并用输出文件的名称重命名输出中的列名。除了使用文件名重命名输出外,我可以完成所有任务。我有以下错误可能是由旧版熊猫引起的。有没有人知道如何解决这个问题,将更新的熊猫更新到新版本?如何解决AttributeError:'DataFrame'对象没有属性'分配'与更新熊猫?
错误
Traceback (most recent call last):
File "multijoin_2.py", line 19, in <module>
result = merge_files(files).reset_index()
File "multijoin_2.py", line 11, in merge_files
pd.read_csv(f, sep='\t', usecols=['r_id', 'exp'])
File "https://stackoverflow.com/users/xxx/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2007, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'assign'
输入
$猫test1的
r_id g_id exp
r1 g1 20
r2 g1 30
r3 g1 1
r4 g1 3
$猫test2的
r_id gid exp
r1 g2 20
r2 g2 30
r3 g2 1
r4 g2 3
$猫TEST3
r_id g_id exp
r1 g3 30
r2 g3 40
r3 g3 11
r4 g3 32
期望输出继电器
r_id test3 test2 test1
0 r1 30 20 20
1 r2 40 30 30
2 r3 11 1 1
3 r4 32 3 3
工作代码(除列命名)
import os
import glob
import pandas as pd
files = glob.glob(r'/path/test*')
def merge_files(files, **kwargs):
dfs = []
for f in files:
dfs.append(
pd.read_csv(f, sep='\t', usecols=['r_id', 'exp'])
#.assign(col=0)
.rename(columns={'col_name':os.path.splitext(os.path.basename(f))[0]})
.set_index(['repeat_id'])
)
return pd.concat(dfs, axis=1)
result = merge_files(files).reset_index()
print(result)
如果省略'#.assign (col = 0)'它不起作用? – jezrael
它的作品,但它只是不重命名的列。 – user1703276