1

I have a empty pandas DataFrame:

aqi_df = pd.DataFrame(columns = ["IMEI","Date","pm10conc_24hrs","pm25conc_24hrs","sdPm10","sdPm25","aqi","windspeed","winddirection","severity","health_impact"] )

I want to add elements one by one to each column -

for i in range(1,10):
   aqi_df.IMEI.append("a")
   aqi_df.Date.append("b")
   aqi_df.pm10conc_24hrs.append("c")
   .
   .
   .

But append throws an error

TypeError: cannot concatenate a non-NDFrame object

How can I append elements to pandas dataframe one by one?

1
  • The error is clear, you need to pass a Series or a DataFrame for appending, besides why do this when it's non-performant as you'll iteratively grow the df on each append call so it'll be slow Commented Jun 13, 2016 at 9:07

1 Answer 1

1

IIUC you can use:

aqi_df = pd.DataFrame(columns = ["IMEI","Date","pm10conc_24hrs"] )
print (aqi_df)


for i in range(1,10):
   aqi_df.loc[i] = ['a','b','c']

print (aqi_df)
  IMEI Date pm10conc_24hrs
1    a    b              c
2    a    b              c
3    a    b              c
4    a    b              c
5    a    b              c
6    a    b              c
7    a    b              c
8    a    b              c
9    a    b              c

But better is creating DataFrame from Series or dict:

IMEI = pd.Series(['aa','bb','cc'])
Date = pd.Series(['2016-01-03','2016-01-06','2016-01-08'])
pm10conc_24hrs = pd.Series(['w','e','h'])

aqi_df = pd.DataFrame({'a':IMEI,'Date':Date,'pm10conc_24hrs':pm10conc_24hrs})
print (aqi_df)
         Date   a pm10conc_24hrs
0  2016-01-03  aa              w
1  2016-01-06  bb              e
2  2016-01-08  cc              h

aqi_df = pd.DataFrame({'a':['aa','bb','cc'],
                       'Date':['2016-01-03','2016-01-06','2016-01-08'],
                       'pm10conc_24hrs':['w','e','h']})
print (aqi_df)
         Date   a pm10conc_24hrs
0  2016-01-03  aa              w
1  2016-01-06  bb              e
2  2016-01-08  cc              h
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion @jezrael

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.