读取 Excel 个文件并跳过空行
Read Excel file and skip empty rows
我有一个 excel sheet 包含如下所示的数据,它包含数据列。
Rahul e34 Pradeep e44 Azhar t54
Venkat r45 Akash e14 Vipul r15 Fairo e45
Akshay e44
Pavan e24 Asad t14
当我运行下面的代码
import pandas as pd
import numpy as np
df = pd.read_excel (r'C:\Users\Kiran\Desktop\Data\Output1.xlsx')
df=pd.DataFrame(np.reshape(df.to_numpy(),(-1,2)))
df.to_excel("Output2.xlsx")
我的输出为
0 1
0 Rahul e34
1 Pradeep e44
2 Azhar t54
3
4 Venkat r45
5 Akash e14
6 Vipul r15
7 Fairo e45
8 Akshay e44
9
10
11
12 Pavan e24
13 Asad t14
14
15
我需要输出如下图
Rahul e34
Pradeep e44
Azhar t54
Venkat r45
Akash e14
Vipul r15
Fairo e45
Akshay e44
Pavan e24
Asad t14
第 1 行 (0,1) 和第 1 列 (0,1,2,3,4.....15) 不应与如何删除空格一起出现。谁能指导我。感谢您的帮助。
您可以在 df.to_excel()
中使用 index
和 header
参数。
在将 excel 读入 pandas DF 时也使用 header=None
。如果不这样做,您将在输出文件中丢失观察结果。
这是工作代码:
import pandas as pd
import numpy as np
df = pd.read_excel ('input.xlsx',header=None)
df=pd.DataFrame(np.reshape(df.to_numpy(),(-1,2)))
df.dropna(axis=0,inplace=True)
clean_df=df[:].astype('str').apply(lambda x: x.str.strip())
print(clean_df)
df.to_excel("Output.xlsx",index=False,header=False)
我有一个 excel sheet 包含如下所示的数据,它包含数据列。
Rahul e34 Pradeep e44 Azhar t54
Venkat r45 Akash e14 Vipul r15 Fairo e45
Akshay e44
Pavan e24 Asad t14
当我运行下面的代码
import pandas as pd
import numpy as np
df = pd.read_excel (r'C:\Users\Kiran\Desktop\Data\Output1.xlsx')
df=pd.DataFrame(np.reshape(df.to_numpy(),(-1,2)))
df.to_excel("Output2.xlsx")
我的输出为
0 1
0 Rahul e34
1 Pradeep e44
2 Azhar t54
3
4 Venkat r45
5 Akash e14
6 Vipul r15
7 Fairo e45
8 Akshay e44
9
10
11
12 Pavan e24
13 Asad t14
14
15
我需要输出如下图
Rahul e34
Pradeep e44
Azhar t54
Venkat r45
Akash e14
Vipul r15
Fairo e45
Akshay e44
Pavan e24
Asad t14
第 1 行 (0,1) 和第 1 列 (0,1,2,3,4.....15) 不应与如何删除空格一起出现。谁能指导我。感谢您的帮助。
您可以在 df.to_excel()
中使用 index
和 header
参数。
在将 excel 读入 pandas DF 时也使用 header=None
。如果不这样做,您将在输出文件中丢失观察结果。
这是工作代码:
import pandas as pd
import numpy as np
df = pd.read_excel ('input.xlsx',header=None)
df=pd.DataFrame(np.reshape(df.to_numpy(),(-1,2)))
df.dropna(axis=0,inplace=True)
clean_df=df[:].astype('str').apply(lambda x: x.str.strip())
print(clean_df)
df.to_excel("Output.xlsx",index=False,header=False)