Python To Save Needed Rows In Excel Contents
I am using Windows 7 + Python 2.76. I am trying to save specific contents of xls files into new files. The original contents looks like: What I want to do is to save all the rows
Solution 1:
With pandas
:
import pandas as pdorig_df= pd.read_excel(orig_excel_path, sheetname=sheetname)
orig_df[orig_df['Visited'] == 'UK'].to_excel(new_excel_path, sheet_name=new_sheetname)
Breaking it down:
orig_df['Visited'] == 'UK'
returns a list of True
or False
for each row if the Visited
column is 'UK'
. In this case [False, True, False, True]
. Passing this list back to the original dataframe will give us only the rows in the indexes corresponding to those with True
.
Solution 2:
to append 1 row with 3 columns, use a "2D array" data structure, e.g. a list of lists:
contents.append([a, b, c])
Post a Comment for "Python To Save Needed Rows In Excel Contents"