Python-117 对excel文件执行trim函数,删除fr
2021-08-27 本文已影响0人
RashidinAbdu
-
具体代码与解释如下:即读取一个xlsx文件,对其进行删除free space,即excel里的trim函数,来执行:
- trim函数的代码:
cols = df.select_dtypes(['object']).columns # Trim the free spaces in front of the data
df[cols] = df[cols].apply(lambda x: x.str.strip())
print (df)
-
具体代码如下:
import pandas as pd
df = pd.read_excel('D:\\USB-GCMS-FILES\\SCFA-SPME-OF_EACH_STRAIN-English-all.xlsx',sheet_name='GCMS-DATA')
cols = df.select_dtypes(['object']).columns # Trim the free spaces in front of the data
df[cols] = df[cols].apply(lambda x: x.str.strip())
print (df)
#df1=df.sort_index(axis=1)
#print(df1)
#df.sort_values(by='ID')
print(",\,")
df1=df.sort_values(by=['Name'])
print(df1)
df1.to_excel("D:\\USB-GCMS-FILES\\SCFA-SPME-OF_EACH_STRAIN-English-all-Sorted.xlsx")