let say i have this dataframe df
| Name | Age |
-----------------
| john | 20 |
| gibbs | 18 |
| jane | 25 |
writing them in an .xlxs file using openpyxl
by column
from openpyxl import Workbook
def write_by_col(ws, col, col_no):
row = 1
for data in col:
ws.cell(row=row, column=col_no).value = data
row += 1
names = df.loc[:, 'Name']
ages = df.loc[:, 'Age']
wb = Workbook()
ws = wb.active
write_by_col(ws, names, 1)
write_by_col(ws, ages, 2)
wb.save('E:\test.xlsx')
Can I safely always assume that what is written on the test.xlsx
, is similar on the df
dataframe like john
has age of 20
and gibbs
has 18
and lastly jane
has 25
?
I’m new in pandas, but it’s seem to me that dataframe
and series
are ordered and it always preserve their ordering, unless of course there are changes, but I’m still want to confirm if my assumptions are correct, since it’s not explicitly stated in their doc, though I didn’t read all of it yet.
I’m aware that I can do pandas.Series.to_excel
, but using openpyxl
give me much more control.
6
The ordering of rows and columns in Pandas data frames is always preserved unless you explicitly change it. This may not be documented in a single place but it is an integral part of how Pandas works and implied in the documentation of various operations.
For example the documentation of pandas.Series.sort_values
states that it returns a new pd.Series
(or None
). That would not make sense if the order of values did not keep their order.