Home > Back-end >  How to use pandas to modify rows in an Excel file by condition?
How to use pandas to modify rows in an Excel file by condition?

Time:04-19

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.

the proper content of the excel file may like this:

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?

I'm not much familiar with python, please let me know if I missed providing any other information. Thanks for your time!

CodePudding user response:

You can reshape your dataframe like this:

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']]

  • Related