【问题标题】:Select a (non-indexed) column based on text content of a cell in a python/pandas dataframe根据 python/pandas 数据框中单元格的文本内容选择(非索引)列
【发布时间】:2019-07-06 08:07:30
【问题描述】:

TL:DR - 如何根据包含特定文本的列从现有非索引数据框中的一列或多列创建数据框/系列? p>

对 Python 和数据分析相对较新,并且(这是我第一次在 Stack Overflow 上发布问题,但我一直在寻找答案很长时间(并且习惯于定期编码)并且没有任何成功。

我有一个从没有命名/索引列的 Excel 文件导入的数据框。我正在尝试从近 2000 个这些文件中成功提取数据,这些文件的数据列都略有不同(当然 - 为什么要简单......或者遵循模板......或者只是使用格式不正确的 Excel 电子表格以外的东西。 ..)。

原始数据帧(来自结构不佳的 XLS 文件)看起来有点像这样:

0                                       NaN             RIGHT      NaN   
1                                      Date              UCVA      Sph   
2                       2007-01-13 00:00:00              6/38  [-2.00]   
3                       2009-11-05 00:00:00               6/9      NaN   
4                       2009-11-18 00:00:00              6/12      NaN   
5                       2009-12-14 00:00:00               6/9  [-1.25]   
6                       2018-04-24 00:00:00           worn CL  [-5.50]   

           3     4      5                 6     7     8        9   \
0         NaN   NaN    NaN               NaN   NaN   NaN      NaN   
1         Cyl  Axis  BSCVA  Pentacam remarks    K1    K2  K2 back   
2     [-2.75]    65    6/9               NaN   NaN   NaN      NaN   
3         NaN   NaN    NaN               NaN   NaN   NaN      NaN   
4         NaN   NaN    6/5         Pentacam     46  43.9     -6.6   
5     [-5.75]    60  6/6-1               NaN   NaN   NaN      NaN   
6     [+7.00}   170  6/7.5               NaN   NaN   NaN      NaN   

           ...              17                18    19    20       21     22  \
0          ...             NaN               NaN   NaN   NaN      NaN    NaN   
1          ...           BSCVA  Pentacam remarks    K1    K2  K2 back  K max   
2          ...             6/5               NaN   NaN   NaN      NaN    NaN   
3          ...             NaN               NaN   NaN   NaN      NaN    NaN   
4          ...             NaN          Pentacam  44.3  43.7     -6.2   45.5   
5          ...           6/4-4               NaN   NaN   NaN      NaN    NaN   
6          ...             6/5               NaN   NaN   NaN      NaN    NaN   

我想提取一组数据帧/系列,然后我可以将它们组合在一起以获得“整洁”的数据帧,例如:

1                                      Date              R-UCVA      R-Sph   
2                       2007-01-13 00:00:00              6/38  [-2.00]   
3                       2009-11-05 00:00:00               6/9      NaN   
4                       2009-11-18 00:00:00              6/12      NaN   
5                       2009-12-14 00:00:00               6/9  [-1.25]   
6                       2018-04-24 00:00:00           worn CL  [-5.50]   

1       R-Cyl R-Axis R-BSCVA  R-Penta          R-K1   R-K2  R-K2 back   
2     [-2.75]    65    6/9               NaN   NaN   NaN      NaN   
3         NaN   NaN    NaN               NaN   NaN   NaN      NaN   
4         NaN   NaN    6/5         Pentacam     46  43.9     -6.6   
5     [-5.75]    60  6/6-1               NaN   NaN   NaN      NaN   
6     [+7.00}   170  6/7.5               NaN   NaN   NaN      NaN  

等等。等等,所以我正在尝试编写一些代码,这些代码将通过查找“日期”或“UCVA”等词来拉出我定义的一系列列。然后我计划将它们重新缝合到一个数据帧中患者标识符作为额外的列。然后循环浏览所有 XLS 文件,将全部附加到单个 CSV 文件中,然后我可以在上面做有用的事情(比如放入 Access 数据库 - 是的,我知道,但它必须易于使用并且已经安装在 NHS 计算机上进行统计分析)。

有什么建议吗?我希望这是足够的信息。

非常感谢。

亲切的问候 维姬

【问题讨论】:

  • 我是否正确理解了这个问题:您想从 Dataframe 中提取一组列到一个新的 Dataframe 中,然后将大量 Dataframes 合并在一起?你想将它们合并到一列还是堆叠它们?
  • 看起来一个不错的起点是在读取文件时使用headerskiprows 参数,假设每个参数的格式相似。这将使您的列编入索引,您可以从那里选择您想要的列

标签: python pandas dataframe


【解决方案1】:

这里有一些东西可以帮助您入门。 我准备了一个text.xlsx 文件: 我可以阅读如下

    path = 'text.xlsx'

    df = pd.read_excel(path, header=[0,1])

    # Deal with two levels of headers, here I just join them together crudely 
    df.columns = df.columns.map(lambda h: '  '.join(h))

    # Slight hack because I messed with the column names
    # I create two dataframes, one with the first column, one with the second column
    df1 = df[[df.columns[0],df.columns[1]]]
    df2 = df[[df.columns[0], df.columns[2]]]

    # Stacking them on top of each other
    result = pd.concat([df1, df2])
    print(result)

    #Merging them on the Date column
    result = pd.merge(left=df1, right=df2, on=df1.columns[0])
    print(result)

这给出了输出

  RIGHT  Sph RIGHT  UCVA       Unnamed: 0_level_0  Date
0        NaN              6/38      2007-01-13 00:00:00
1        NaN              6/37      2009-11-05 00:00:00
2        NaN              9/56      2009-11-18 00:00:00
0    [-2.00]               NaN      2007-01-13 00:00:00
1        NaN               NaN      2009-11-05 00:00:00
2        NaN               NaN      2009-11-18 00:00:00

  Unnamed: 0_level_0  Date RIGHT  UCVA       RIGHT  Sph
0      2007-01-13 00:00:00              6/38    [-2.00]
1      2009-11-05 00:00:00              6/37        NaN
2      2009-11-18 00:00:00              9/56        NaN

一些提示: 如何合并两个标题行?见this问答。

如何有条件地选择 pandas 列?参见例如thisthis

如何合并数据框? pandas里有很好的攻略doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2011-01-21
    • 2021-11-10
    相关资源
    最近更新 更多