I have defined an empty data frame with
JavaScript
x
2
1
df = pd.DataFrame(columns=['Name', 'Weight', 'Sample'])
2
and want to append rows in a for loop like this:
JavaScript
1
5
1
for key in my_dict:
2
3
row = {'Name':key, 'Weight':wg, 'Sample':sm}
4
df = pd.concat(row, axis=1, ignore_index=True)
5
But I get this error
JavaScript
1
2
1
cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
2
If I use df = df.append(row, ignore_index=True)
, it works but it seems that append
is deprecated. So, I want to use concat()
. How can I fix that?
Advertisement
Answer
You can transform your dict in pandas DataFrame
JavaScript
1
8
1
import pandas as pd
2
df = pd.DataFrame(columns=['Name', 'Weight', 'Sample'])
3
for key in my_dict:
4
5
#transform your dic in DataFrame
6
new_df = pd.DataFrame([row])
7
df = pd.concat([df, new_df], axis=0, ignore_index=True)
8