Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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_Python 2.7_Pandas - Fatal编程技术网

Python 合并数据帧中数量不确定的行

Python 合并数据帧中数量不确定的行,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有一个CSV文件,看起来像这样: tid || instr_count || fnname ============================= 22 || 892806 || main 22 || 18 || randlc 22 || 120 || makea tid || instr_count || fnname ============================= 22 || 892806 || m

我有一个CSV文件,看起来像这样:

tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||          18  || randlc
22  ||         120  || makea
tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||         138  || makea
我想根据给定列表中是否出现
fnname
instr\u count
的值合并在一起。例如,如果我的列表是
['main','makea']
,则最终的表应该如下所示:

tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||          18  || randlc
22  ||         120  || makea
tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||         138  || makea
我事先不知道给定列表中有多少条目介于2个值之间-因此可能更类似于此:

tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||          18  || randlc
22  ||           7  || randlc
22  ||          35  || randlc
22  ||          20  || randlc
22  ||         120  || makea
应将其压缩为:

tid ||  instr_count || fnname
=============================
22  ||      892806  || main
22  ||         200  || makea
我已经使用pandas 0.17.1和python 2.7.6将这些值加载到了一个
数据帧中。以下是我目前掌握的情况:

def compressDataframes(df):

    new_df = pd.DataFrame(columns=df.columns)
    instr_count = 0
    i = 0
    for row in df.itertuples():
        instr_count += row[2]
        if any(f in row[3] for f in FUNCS): #FUNCS is my "given list"
            new_df.loc[i] = [row[1], instr_count, row[3]]
            i += 1
            instr_count = 0

    return new_df
这是可行的,但我怀疑一定有一种更快的方法(我正在处理一些非常大(>10GB)的数据集)。有人有什么建议吗?

我想您可以使用来创建新的列
分组
,该列首先包含
NaN
,其中没有数据,然后由有效的观察值填充空白(回填)。最后是列
instr\u count的聚合:

li = ['main','makea']

df['grouped'] = df.loc[df['fnname'].isin(li), 'fnname']

df['grouped'] = df['grouped'].fillna(method='bfill')

print df
   tid  instr_count  fnname grouped
0   22       892806    main    main
1   22           18  randlc   makea
2   22          120   makea   makea

print df.groupby(['tid','grouped'])['instr_count'].sum().reset_index()
   tid grouped  instr_count
0   22    main       892806
1   22   makea          138
或与:

第二个样本:

li = ['main','makea']
df['grouped'] = df.loc[df['fnname'].isin(li), 'fnname']
df['grouped'] = df['grouped'].fillna(method='bfill')

print df
   tid  instr_count  fnname grouped
0   22       892806    main    main
1   22           18  randlc   makea
2   22            7  randlc   makea
3   22           35  randlc   makea
4   22           20  randlc   makea
5   22          120   makea   makea

print df.groupby(['tid','grouped'])['instr_count'].sum().reset_index()
  grouped  tid  instr_count
0    main   22       892806
1   makea   22          200

print df.groupby('grouped').agg({'tid':'first', 'instr_count': sum}).reset_index()
   tid grouped  instr_count
0   22    main       892806
1   22   makea          200

刚刚意识到-如果我有像
main、randlc、randlc、makea、makea这样的东西,我认为这不会正常工作。第一个
makea
将被分组到第二个中,不是吗?是否可以将行号复制到分组列中?这将产生一个唯一的标识符,然后可以对其进行回填。^通过添加一个索引行并将其用作列而不是
'fnname'
来实现这一点。再次感谢您的回答!