我的客户向我发送了一个数据材料表.xlsx,其中有 N 行和 N 列,但具体来说我只需要 12 列。
他第一次发送Materialsheet.xlsx
材质 | 长德 | 参考 |
---|---|---|
11000021 | 熊猫玩具 | 棉质玩具 |
11100212 | 小熊猫玩具 | 塑料玩具 |
他第二次发送Materialsheet.xlsx
内部 | 描述 | 地点 |
---|---|---|
11000021 | 熊猫玩具 | 棉质玩具 |
11100212 | 小熊猫玩具 | 塑料玩具 |
我的代码:
ds1 = pd.read_excel('Materialsheet.xlsx', usecols=['Material', 'Group', 'Priority', 'Long Des', 'Ref', 'Qty','Shape'],index_col=False)
ds1 = ds1[['Material', 'Group','Priority','Long Des','Qty','Shape','Ref']]
print(ds1)
或
ds = pd.read_excel('Materialsheet.xlsx', usecols=['Internal', 'Alter.Group', 'Priority', 'Describe', 'Location', 'Qty','image'],index_col=False)
` 我总是更改我的 Excel 标题或代码行来获取输出
我需要一个用于这种标头字段的代码,我要更改标头字段名称。
这可能需要支持吗?
只需使用 .rename() 方法:
ds1.rename(columns={'material': 'PN'} inplace=True)
ds.rename(columns={'Internal': 'PN'}, inpalce=True)
您还可以编写自定义函数,以始终更改列名称,无论您使用哪个文件:
def rename_column(df):
columns_list = list(df.column)
if 'material' in column_list:
df.rename(columns={'material': 'PN'}, inplace=True)
elif 'Internal' in column_list:
df.rename(columns={'Internal': 'PN'}, inplace=True)
return df