I currently have a very redundant code to address multiple columns of a row from a pandas dataframe. Which looks something like this:
info = []
info.append(row['column name'])
...
(multiple lines of mere appending)
And I am passing this list on to another python script that populates the returned values on a web page. I have to improve this to get rid of redundancy, and I am not sure how to go about this.
What is the best practice to do such task?
You can pass a list of column names.
import pandas as pd
df = pd.DataFrame({'x':[1, 2, 3], 'y':[5, 6, 7], 'z':['a', 'b', 'c']})
select a row with iloc
and select columns using list of names, eg if you only want 'x' and 'z':
df.iloc[1][['x', 'z']]
returns:
x 2
z b
Name: 1, dtype: object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.