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

Python:在列表中查找相似字符串的算法

Python:在列表中查找相似字符串的算法,python,pandas,Python,Pandas,我不能用这个东西来组织我的想法。我希望你能帮助我。 我有这样一份财务报告: CONSOLIDATED BALANCE SHEETS - USD ($) $ in Millions Sep. 28, 2019 Sep. 29, 2018 0 Current assets: NaN NaN 1 Cash and cash equival

我不能用这个东西来组织我的想法。我希望你能帮助我。 我有这样一份财务报告:

CONSOLIDATED BALANCE SHEETS - USD ($) $ in Millions Sep. 28, 2019 Sep. 29, 2018
0                                     Current assets:            NaN           NaN
1                           Cash and cash equivalents          48844         25913
2                               Marketable securities          51713         40388
3                            Accounts receivable, net          22926         23186
4                                         Inventories           4106          3956
5                        Vendor non-trade receivables          22878         25809
6                                Other current assets          12352         12087
7                                Total current assets         162819        131339
8                                 Non-current assets:            NaN           NaN
9                               Marketable securities         105341        170799
10                 Property, plant and equipment, net          37378         41304
11                           Other non-current assets          32978         22283
12                           Total non-current assets         175697        234386
13                                       Total assets         338516        365725
14                               Current liabilities:            NaN           NaN
15                                   Accounts payable          46236         55888
16                          Other current liabilities          37720         33327
17                                   Deferred revenue           5522          5966
18                                   Commercial paper           5980         11964
19                                          Term debt          10260          8784
20                          Total current liabilities         105718        115929
21                           Non-current liabilities:            NaN           NaN
22                                          Term debt          91807         93735
23                      Other non-current liabilities          50503         48914
24                      Total non-current liabilities         142310        142649
25                                  Total liabilities         248028        258578
26                      Commitments and contingencies                             
27                              Shareholders’ equity:            NaN           NaN
28  Common stock and additional paid-in capital, $...          45174         40201
29                                  Retained earnings          45898         70400
30      Accumulated other comprehensive income/(loss)           -584         -3454
31                         Total shareholders’ equity          90488        107147
32         Total liabilities and shareholders’ equity         338516        365725
这是一个从excel读取的数据框。我想通过某种算法获得以下输出:

CONSOLIDATED BALANCE SHEETS - USD ($) $ in Millions Sep. 28, 2019 Sep. 29, 2018
0                          Cash and cash equivalents          48844         25913
1                               Total current assets         162819        131339
2                 Property, plant and equipment, net          37378         41304
3                           Total non-current assets         175697        234386
4                                       Total assets         338516        365725
5                                   Accounts payable          46236         55888
6                          Total current liabilities         105718        115929
                                          Total debt         108047        114483
7                      Total non-current liabilities         142310        142649
8                                  Total liabilities         248028        258578
9                         Total shareholders’ equity          90488        107147
基本上,使用给定的键值,在DataFrame的第一列中搜索并返回每个匹配的行。只使用一个数据帧很容易,因为键值与搜索的值完全相同。但事实并非如此。我有数千份报告,其中搜索的值略有不同。e、 g:键值=
现金
,df中的值=
现金和现金等价物
,键值=
净销售额
,df中的值=
净收入
到目前为止我试过什么?
我尝试了
fuzzyfuzzy
模块,但有时它不能正常工作。有什么想法吗?

处理此类搜索的一种方法是添加分类名称,以便于缩小范围。如果您想知道流动资产的总额,可以提取“类别1”作为流动资产,提取“flg”作为总额,最好使用,也可以使用
str.contains()
执行模糊搜索。 注意:在创建代码时,列名已更改

df.replace('NaN', np.NaN, inplace=True)
df.rename(columns={'CONSOLIDATED BALANCE SHEETS - USD ($) $ in Millions':'accounts','Sep. 28, 2019':'this_year','Sep. 29, 2018':'last_year'}, inplace=True)
df['NO'] = np.arange(len(df))
df['Class1'] = df['accounts'][df.isnull().any(axis=1)]
df['Class1'] = df['Class1'].fillna(method='ffill')
df['flg'] = np.where(df['accounts'].str.contains(r'^(Total)'), 'total', 'items')

df
例如:
str.contains()


查看[fuzzwuzzy]{),它似乎为每对匹配的字符串输出了一个比率。(1)是否有任何标准来决定是否应该从输出中包括或排除字符串?(2)是否有任何阈值
fuzzy
'match`
ratio
比如
=0.50
,您可能发现该阈值工作得更好,(3)是否有可能知道其他报告与问题中发布的报告有多大不同,即相同的短语或单词是否会出现在其他报告中?谢谢。这是一个很好的起点!
|    | accounts                                          |   this_year |   last_year |   NO | Class1                        | flg   |
|---:|:--------------------------------------------------|------------:|------------:|-----:|:------------------------------|:------|
|  0 | Current assets:                                   |         nan |         nan |    0 | Current assets:               | items |
|  1 | Cash and cash equivalents                         |       48844 |       25913 |    1 | Current assets:               | items |
|  2 | Marketable securities                             |       51713 |       40388 |    2 | Current assets:               | items |
|  3 | Accounts receivable, net                          |       22926 |       23186 |    3 | Current assets:               | items |
|  4 | Inventories                                       |        4106 |        3956 |    4 | Current assets:               | items |
|  5 | Vendor non-trade receivables                      |       22878 |       25809 |    5 | Current assets:               | items |
|  6 | Other current assets                              |       12352 |       12087 |    6 | Current assets:               | items |
|  7 | Total current assets                              |      162819 |      131339 |    7 | Current assets:               | total |
|  8 | Non-current assets:                               |         nan |         nan |    8 | Non-current assets:           | items |
|  9 | Marketable securities                             |      105341 |      170799 |    9 | Non-current assets:           | items |
| 10 | roperty, plant and equipment, net                 |       37378 |       41304 |   10 | Non-current assets:           | items |
| 11 | Other non-current assets                          |       32978 |       22283 |   11 | Non-current assets:           | items |
| 12 | Total non-current assets                          |      175697 |      234386 |   12 | Non-current assets:           | total |
| 13 | Total assets                                      |      338516 |      365725 |   13 | Non-current assets:           | total |
| 14 | Current liabilities:                              |         nan |         nan |   14 | Current liabilities:          | items |
| 15 | Accounts payable                                  |       46236 |       55888 |   15 | Current liabilities:          | items |
| 16 | Other current liabilities                         |       37720 |       33327 |   16 | Current liabilities:          | items |
| 17 | Deferred revenue                                  |        5522 |        5966 |   17 | Current liabilities:          | items |
| 18 | Commercial paper                                  |        5980 |       11964 |   18 | Current liabilities:          | items |
| 19 | Term debt                                         |       10260 |        8784 |   19 | Current liabilities:          | items |
| 20 | Total current liabilities                         |      105718 |      115929 |   20 | Current liabilities:          | total |
| 21 | Non-current liabilities:                          |         nan |         nan |   21 | Non-current liabilities:      | items |
| 22 | Term debt                                         |       91807 |       93735 |   22 | Non-current liabilities:      | items |
| 23 | Other non-current liabilities                     |       50503 |       48914 |   23 | Non-current liabilities:      | items |
| 24 | Total non-current liabilities                     |      142310 |      142649 |   24 | Non-current liabilities:      | total |
| 25 | Total liabilities                                 |      248028 |      258578 |   25 | Non-current liabilities:      | total |
| 26 | Commitments and contingencies                     |         nan |         nan |   26 | Commitments and contingencies | items |
| 27 | Shareholders’ equity:                             |         nan |         nan |   27 | Shareholders’ equity:         | items |
| 28 | Common stock and additional paid-in capital, $... |       45174 |       40201 |   28 | Shareholders’ equity:         | items |
| 29 | Retained earnings                                 |       45898 |       70400 |   29 | Shareholders’ equity:         | items |
| 30 | Accumulated other comprehensive income/(loss)     |        -584 |       -3454 |   30 | Shareholders’ equity:         | items |
| 31 | Total shareholders’ equity                        |       90488 |      107147 |   31 | Shareholders’ equity:         | total |
| 32 | Total liabilities and shareholders’ equity        |      338516 |      365725 |   32 | Shareholders’ equity:         | total |
df[df['accounts'].str.contains('Accounts payable')]

    accounts    this_year   last_year   NO  Class1  flg
15  Accounts payable    46236.0 55888.0 15  Current liabilities:    items