简体   繁体   中英

Append a row to pandas dataframe for select columns only

I'd like to append a new row to pandas DataFrame, but only populate select columns. In the code below, I subset the columns list I'd like to populate and assign a list of values.

import pandas as pd

sampleDF = pd.DataFrame(columns=['Tenant','Industry','Square Footage'])

sampleDF = sampleDF.iloc[sampleDF.tail(1).index.item(), ['Tenant', 'Industry']] = ['DE Shaw', 'Finance']

The above code doesn't work.

You can use the append() method by inserting a dictionary. Does this help?

sampleDF = sampleDF.append({'Tenant': 'De Shaw', 'Industry': 'Finance'}, ignore_index = True)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM