[英]How to use pandas to modify rows in an Excel file by condition?
I got an excel file full with wrong arrangement, and I think it is possiable to speed up my correction by using python and pandas.我得到一个 excel 的文件,但文件排列错误,我认为使用 python 和 pandas 可以加快我的更正速度。
the proper content of the excel file may like this: excel 文件的正确内容可能是这样的:
Name![]() |
Gender![]() |
Age![]() |
Group![]() |
---|---|---|---|
Tom![]() |
Male![]() |
22 ![]() |
A![]() |
Liu![]() |
Male![]() |
19 ![]() |
C ![]() |
Kim![]() |
Female![]() |
30 ![]() |
B![]() |
but now it is like this:但现在是这样的:
Name![]() |
Gender![]() |
Age![]() |
Group![]() |
---|---|---|---|
Tom![]() |
Male![]() |
||
22 ![]() |
A![]() |
||
Liu![]() |
Male![]() |
||
19 ![]() |
C ![]() |
||
Kim![]() |
Female![]() |
||
30 ![]() |
B![]() |
So far I've learn about the basic operation for pandas, is there any function in pandas to solve it?至此了解了pandas的基本操作,请问pandas中有function可以解决吗?
I'm not much familiar with python, please let me know if I missed providing any other information.我对 python 不太熟悉,如果我错过了提供任何其他信息,请告诉我。 Thanks for your time!
谢谢你的时间!
You can reshape your dataframe like this:您可以像这样重塑 dataframe:
df = pd.DataFrame(df.iloc[:, :2].to_numpy().reshape(-1, 4), columns=df.columns)
print(df)
# Output
Name Gender Age Group
0 Tom Male 22 A
1 Liu Male 19 C
2 Kim Female 30 B
You can replace df.iloc[:, :2]
with df[['Name', 'Gender']]
您可以将
df.iloc[:, :2]
替换为df[['Name', 'Gender']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.