python - 根据 python/pandas 数据框中单元格的文本内容选择(非索引)列

标签 python pandas dataframe

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

对 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 计算机上 - 和统计分析)。

有什么建议吗?我希望这些信息已经足够了。

提前非常感谢。

亲切的问候 维姬

最佳答案

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

    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

关于python - 根据 python/pandas 数据框中单元格的文本内容选择(非索引)列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54654126/

相关文章:

r - 使用非连续行号的子集数据

r - 为什么在读取数据框时我的列名称中出现 X.?

python - 无法访问 annotate() 中的 to_attr 属性

python - 为什么我的正则表达式用函数对象而不是替换值替换匹配模式?

python - 自动覆盖 pandas 数据框中的列名称

python - 用于复制一行以填充 DataFrame 的 Pandas

r - 影响 R 数据帧中的值而不检查索引是否为空

python - groupby 上的复杂/多列操作

python - 使用 Python 和 Selenium 按标题查找并单击元素

python - 按列分组并找到每组的最小值和最大值