Python:根据另一列中的值提取单元格值

时间:2016-12-30 14:32:05

标签: python excel pandas indexing openpyxl

每当F列(蓝色)中有#行时,我需要从D列(黄色)中提取字符串。我是初学者,正在尝试Pandas和openpyxl来完成这项任务,但没有运气。哪一个更适合这个?
我希望它们存储好,以便我以后可以访问它们   此外,使用正则表达式从H列(绿色)中提取数字最简单吗? Link to onedrive with the excel My workbook

2 个答案:

答案 0 :(得分:2)

我认为您首先需要read_excel,并且似乎必须首先跳过Source: local data frame [30 x 10] carat cut color clarity depth table price x y z <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 7 : : I VVS1 62.3 57 336 3.95 3.98 2.47 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 11 0.3 Good J SI1 64 55 339 4.25 4.28 2.73 12 0.23 Ideal J VS1 62.8 56 340 3.93 3.9 2.46 13 0.22 Premium F SI1 60.4 61 342 3.88 3.84 2.33 14 0.31 Ideal J SI2 62.2 54 344 4.35 4.37 2.71 15 0.2 Premium E SI2 60.2 62 345 3.79 3.75 2.27 16 0.32 Premium E I1 60.9 58 345 4.38 4.42 2.68 17 0.3 Ideal I SI2 62 54 348 4.31 4.34 2.68 18 : Good J SI1 63.4 54 351 4.23 4.29 2.7 19 : : : : 63.8 56 351 4.23 4.26 2.71 20 : Very Good J SI1 62.7 59 351 4.21 4.27 2.66 21 : Good I SI2 63.3 56 351 4.26 4.3 2.71 22 0.23 Very Good E VS2 63.8 55 352 3.85 3.92 2.48 23 : : H VS1 61 57 353 3.94 3.96 2.41 24 0.31 Very Good J SI1 59.4 62 353 4.39 4.43 2.62 25 : : : : 58.1 62 353 4.44 4.47 2.59 26 0.23 Very Good G VVS2 60.4 58 354 3.97 4.01 2.41 27 0.24 Premium I VS1 62.5 57 355 3.97 3.94 2.47 28 0.3 Very Good J VS2 62.2 57 357 4.28 4.3 2.67 29 0.23 Very Good D VS2 60.5 61 357 3.96 3.97 2.4 30 : : F VS1 60.9 57 357 3.96 3.99 2.42 行:

7

然后按loc选择boolean indexing

df = pd.read_excel('LTE_KPIs_up.xlsx', skiprows=7)
#print (df)
print (df.loc[df.Unit == '#', 'KPI name'])

答案 1 :(得分:1)

您可以使用以下代码从列F中​​选择所需的值。我还假设列H有一个&#39; =&#39;数字前的标志

import csv
import pandas as pd
from io import StringIO
Excelfile = "file.xlsx"
df = pd.read_excel(open(Excelfile,'rb'), sheetname='Sheet1')
selectstring = df['ColumnD'].where(df['ColumnF'] == '#')
print selectstring

print df['Columnh'].str.split('=')[1]