我正在写一个CSV
数据表,想解析并过滤掉其中的数据,在写代码的时候,我发现有人在SO POST上问过一个类似的代码,作者的H/W数据与我看到的HPE H/W数据几乎相同,我有一些不同的数据和列
Status Server Server Name Bay # Model Processor Proc. Count Memory Serial Number State Power State iLO FW Firmware Appliance Name
Critical enc2010, bay 1 tdm2066.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 262144 2M272101N9 Unmanaged On 2.53 May 03 2017 I36 v2.40 (02/17/2017) OV C7000 enclosures 1
OK enc1011, bay 1 tdm1068.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 262144 2M272101P6 Monitored On 2.55 Aug 16 2017 I36 v2.74 (07/21/2019) OV C7000 enclosures 1
OK enc1012, bay 1 tdm1083.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 262144 2M272101NX Monitored On 2.61 Jul 27 2018 I36 v2.60 (05/21/2018) OV C7000 enclosures 1
OK ENC2004, bay 1 tdm2033.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz 2 524288 2M262602L2 Monitored On 2.55 Aug 16 2017 I36 v2.52 (10/25/2017) OV C7000 enclosures 1
OK ENC2006, bay 1 vds2009 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 524288 2M263604ZZ Monitored On 2.40 Dec 02 2015 I36 v2.20 (05/05/2016) OV C7000 enclosures 1
OK ENC2011, bay 1 tdm2081.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 524288 2M2708027Z Monitored On 2.55 Aug 16 2017 I36 v2.52 (10/25/2017) OV C7000 enclosures 1
OK ENC1003, bay 1 tdm1024.example.com 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz 2 524288 2M262602KW Monitored On 2.73 Feb 11 2020 I36 v2.52 (10/25/2017) OV C7000 enclosures 1
OK ENC1006, bay 1 vds1009 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz 2 524288 2M262505V5 Monitored On 2.40 Dec 02 2015 I36 v2.00 (12/28/2015) OV C7000 enclosures 1
OK ENC1007, bay 1 vds1023 1 ProLiant BL460c Gen9 Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz 2 524288 2M264800TR Monitored On 2.50 Sep 23 2016 I36 v2.30 (09/12/2016) OV C7000 enclosures 1
df = pd.read_csv("testcreate.csv", sep="\t")
df = df[[ 'Server', 'Server Name', 'Bay #', 'Appliance Name']]
df['Bay'] = df['Server'].str.split(',').str[1].str.lower()
df['Enclosure'] = df['Server'].str.split(',').str[0].str.upper()
df['Server Name'] = df['Server Name'].str.split('.').str[0]
df = df.drop(['Server', 'Bay #'], axis=1)
df = df[df['Appliance Name'].str.contains('C7000')]
Server Name Appliance Name Bay Enclosure
0 tdm2066 OV C7000 enclosures 1 bay 1 ENC2010
1 tdm1068 OV C7000 enclosures 1 bay 1 ENC1011
2 tdm1083 OV C7000 enclosures 1 bay 1 ENC1012
3 tdm2033 OV C7000 enclosures 1 bay 1 ENC2004
4 vds2009 OV C7000 enclosures 1 bay 1 ENC2006
5 tdm2081 OV C7000 enclosures 1 bay 1 ENC2011
6 tdm1024 OV C7000 enclosures 1 bay 1 ENC1003
7 vds1009 OV C7000 enclosures 1 bay 1 ENC1006
8 vds1023 OV C7000 enclosures 1 bay 1 ENC1007
9 vds0003 OV C7000 enclosures 1 bay 1 ENT0003
10 tdm7123 OV C7000 enclosures 1 bay 1 ENC7003
11 tdm2231 OV C7000 enclosures 1 bay 1 ENC2022
12 tdm2186 OV C7000 enclosures 1 bay 1 ENC2018
13 tdm1098 OV C7000 enclosures 1 bay 1 ENC1013
14 tdm1158 OV C7000 enclosures 1 bay 1 ENC1017
15 tdm2096 OV C7000 enclosures 1 bay 1 ENC2012
16 tdm1012 OV C7000 enclosures 1 bay 1 ENC1002
17 tdm1062 OV C7000 enclosures 1 bay 1 ENC1009
18 vds1041 OV C7000 enclosures 1 bay 1 ENC1010
19 vds1001 OV C7000 enclosures 1 bay 1 ENC1005
20 vds7025 OV C7000 enclosures 1 bay 1 ENC7009
21 vds2023 OV C7000 enclosures 1 bay 1 ENC2007
22 tdm7068 OV C7000 enclosures 1 bay 1 ENC7005
23 vds7006 OV C7000 enclosures 1 bay 1 ENC7006
24 tdm2126 OV C7000 enclosures 1 bay 1 ENC2014
25 vds2001 OV C7000 enclosures 1 bay 1 ENC2005
26 tdm1173 OV C7000 enclosures 1 bay 1 ENC1018
27 tdm1250 OV C7000 enclosures 1 bay 1 ENC1025
我借了df1 = pd.concat( [g.set_index('Bay').add_suffix(f'_{n}') for n, g in df.groupby('Enclosure')], axis=1, sort=False).filter( like='Server Name').dropna(how='all', axis=1)
<;-这来自于提到的SO帖子,但我没有完全理解它,而且我不想添加后缀ieServer Name
,因此它应该只有like
Enc1002`等
df1 = pd.concat( [g.set_index('Bay').add_suffix(f'_{n}') for n, g in df.groupby('Enclosure')], axis=1, sort=False).filter( like='Server Name').dropna(how='all', axis=1)
print(df1)
结果:
ENC1002 ENC1003 ENC1005
bay 1 tdm1012 tdm1024 vds1001
我从@Scott那里得到了desired
解决方案
df = pd.concat([g.set_index('Bay')['Server Name'].rename(f'{n}') for n, g in df.groupby('Enclosure')], axis=1, sort=False)
我的代码可能有点凌乱,如Dataframe
下所示,有没有更好的编码方法,只是在这里询问它,以获得更好的建议和代码写作
在阅读了您的文章和代码块之后,我看不到操作的太多范围。我想改变它如下,这将提供理想的结果。然而,我在代码审查上看到了类似的帖子
1-您应该选择所需的列,这将减少处理负担和灵活性,您可以使用
usecols
2-您可以将
df.assign
与Dict一起使用,Dict将基于Keys, values
从两个集合创建一个Dict,这将通过拆分,
创建两个不同的列,因此您可以执行split
,rename
&drop
一次性行动它将如下所示,这应该是可行的
如果我理解正确,您希望旋转您的表:
我希望我没弄错
相关问题 更多 >
编程相关推荐