I'm new to pandas, but trying to simply add a row
class Security:
def __init__(self):
self.structure = ['timestamp', 'open', 'high', 'low', 'close', 'vol']
self.df = pd.DataFrame(columns=self.structure) # index =
def whats_inside(self):
return self.df
"""
Some skipped code...
"""
def add_data(self, timestamp, open, high, low, close, vol):
data = [timestamp, open, high, low, close, vol]
self.df = self.df.append (data)
sec = Security()
print sec.whats_inside()
sec.add_data ('2015/06/01', '1', '2', '0.5', '1', '100')
print sec.whats_inside()
but the output is:
0 close high low open timestamp vol
0 2015/06/01 NaN NaN NaN NaN NaN NaN
1 1 NaN NaN NaN NaN NaN NaN
2 2 NaN NaN NaN NaN NaN NaN
3 0.5 NaN NaN NaN NaN NaN NaN
4 1 NaN NaN NaN NaN NaN NaN
5 100 NaN NaN NaN NaN NaN NaN
This means, I'm adding a column instead of row. Yes, I've tried to google but still didnt get the point how do make it simple pythonic way.
p.s. I know that's simple, but I'm just missing something important.
There are several ways to add a new row. Perhaps the easiest one is (if you want to add the row to the end) is to use loc
:
df.loc[len(df)] = ['val_a', 'val_b', .... ]
loc
expects an index. len(df)
will return the number of rows in the dataframe so the new row will be added to the end of the dataframe.
'['val_a', 'val_b', .... ]' is a list of values of the row, in the same order of the columns, so the list's length must be equal to the number of columns, otherwise you will get a ValueError
exception.
An exception for this is that if you want all the columns to have the same values you are allowed to have that value as a single element in the list, for example df.loc[len(df)] = ['aa']
.
NOTE: a good idea will be to always use reset_index
before using this method because if you ever delete a row or work on a filtered dataframe you are not guaranteed that the rows' indexes will be in sync with the number of rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With