Python多功能索引切片

时间:2018-10-25 10:44:02

标签: python pandas indexing data-modeling data-cleaning

希望有人可以帮助我将这个Excel逻辑转换为python

=IF(LEFT(A8,5)="Total",A9,I8)

因此,我希望找到一个范围内的所有内容,然后使用该范围内的第一个元素创建一个新列。问题在于范围的名称可以更改。

我已经实现的当前解决方案是将列转换为索引,并通过执行以下操作手动选择索引名称:

Sales = df.loc['1000 - Cash and Equivalents':'Total - 1000 - Cash and Equivalents']

此名称可能会更改,可能包含更少或更多的行,并且需要使其更通用,因此我无法指定编号范围。

这是数据示例:

enter image description here

和Post转换我的数据如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用:

df = pd.read_csv('PL2.csv', encoding='cp1252', engine='python')


#create helper df for total strings
df1 = df.loc[df.iloc[:, 0].str.startswith('Total', na=False), df.columns[0]].to_frame('total')
#first column without Total - 
df1['first'] = df1['total'].str.replace('Total - ', '')
print (df1.head(10))
                                    total                          first
17                   Total - 4000 - Sales                   4000 - Sales
21  Total - 4200 - Discounts & Allowances  4200 - Discounts & Allowances
24       Total - 4400 - Excise and Duties       4400 - Excise and Duties
25                          Total - Sales                          Sales
37      Total - 5000 - Cost of Goods Sold      5000 - Cost of Goods Sold

#create index by first column
df = df.set_index(df.columns[0])

#filter function - if not matched return empty df
def get_dict(df, first, last):
    try:
        df = df.loc[first: last]
        df['Sub-Category'] = first
    except KeyError:
        df = pd.DataFrame()
    return df

#in dictionary comprehension create dict of DataFrames     
d = {k: get_dict(df, k, v) for k, v in zip(df1['first'], df1['total'])}
#print (d)

#select Sales df
print (d['Sales'])