【问题标题】:Conditional filtering in excel using python使用python在excel中进行条件过滤
【发布时间】:2022-01-01 17:54:08
【问题描述】:

我目前正在尝试对特定列中的数据进行颜色编码,以便在我的程序中表现出色。

当我运行这段代码时:

import pandas
import pandas as pd
import numpy as np
import re
from openpyxl import load_workbook

#opening txt file location, reading the entire file
with open("Data at 20-5C on(18-11-21)(decoupled).txt", "r", encoding="utf8") as f:
    data = f.readlines()

#filters only ad averaged values
data = ([x.strip().split(',') for x in data if "RAW" not in x])
max_len = max([len(i) for i in data])

for row in data:
    if len(row) < max_len:
        row += [''] * (max_len - len(row))

#adding columns
df = pd.DataFrame(data, dtype=int, columns=['', 'TH+', 'Vacm', 'Vout', 'Bat mon', 'TH-', 'Vbat2', 'Vamb', 'VambCorrY'])
#inserting index column
df.insert(1,'Index', np.nan)
#changing index to default index
df['Index'] = df.index

stage_threshold = 2

df_styled = df.style.applymap(lambda x: 'background-color: red' if x < stage_threshold else 'background-color: green', subset=['Vout'])

df_styled.to_excel('Data at 20-5C on(18-11-21)(decoupled).xlsx', engine='openpyxl', index=False)

我受到'&lt;' not supported between instances of 'str' and 'int'的欢迎

以下是 .txt 中的数据示例:

Run1    Tbb= 20 C    Volt=2.938   Tamb= 21.44 C   

 AD Averaged 0129(mV), 0001, 0001, 0001, 0001, 0001, 0001, 0000,
 RAW Values  0129(xx), FFFF, FFFF, FFFF, FFFF, FFFF, FFFF,
 AD Averaged 0169(mV), 2297, 1071, 0001, 2901, 1015, 2500, 0000,
 RAW Values  0169(xx), 0EB2, 06DA, FFFF, 0C60, 067E, 0FFF,
 AD Averaged 01a9(mV), 2297, 1071, 0001, 2900, 1015, 2500, 0000,
 RAW Values  01a9(xx), 0EB2, 06DA, FFFF, 0C5F, 067E, 0FFF,
 AD Averaged 01e9(mV), 2297, 1070, 0001, 2891, 1015, 2500, 0000,

我知道无法根据 int 检查字符串,反之亦然。我想知道如何将我的数据集转换为 int,或者这是否是我应该采取的方法?

谢谢!

【问题讨论】:

    标签: python excel pandas dataframe numpy


    【解决方案1】:

    使用

    df['Vout'] = pd.to_numeric(df['Vout'], errors='coerce').astype('Int64')
    

    我解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2021-04-22
      • 2021-09-16
      • 2018-02-21
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      相关资源
      最近更新 更多