python - How do I update dataframe column values with Pandas? -


Say that I have a bunch of data that comes like this:

  10-12-2014 3.45 10-12-2014 3.67 10-12-2014 4.0 10-12-2014 5.0 10-13-2014 6.0 10-13-2014 8.9  

And so many On.

I want to keep this stuff in Pandu Data Frame in a format like this:

  10-12-2014 3.45 3.57 4.0 5.0 10-13-2014 6.0 8.9 etc. Etc.  

To do this, I currently have something like this:

  rows = cursor.fetchall () df = pd.DataFrame (column) ('Previous' date for row in row, 'val 1', 'val2', 'val 3', 'val 4')) previous_data = 0: if previous_data == 0: df  

And this is where I got stuck - I can not find any way to Irf Adds the values ​​in a date column. I'm also not sure how to update the old column in future iterations, because I'm actually able to find examples of people just adding the entire rows. Or is there a better way to do this?

Working with a string to come in that form you can do the following.

1) Get the data as DF

  d = "" 10-12-2014 3.45 10-12-2014 3.67 10-12-2014 4.0 10 -12-2014 5.0 10-13-2014 6.0 10-13-2014 8.9 "" "df = pd.read_csv (string iso string ido (d), sep =" \ s ", name = ['date', 'v' ])  

2) Create a group and apply a function to indicate its values.

  groups = df.groupby ('date') df = groups.apply (lambda x: x ['V']. Reset_index (drop = true)) The number below is equal to below , But clunkier is equal to usage if you want the sentences as columns #df = groups.apply (lambda x: pd.Series ({"Val {0}" .format (1 + i): For each 3) For each, reset index to reset the indexed reset column and column as your date, for each, n (n ['v']})  

3). ..

  df = df.unstack (level = 1) df = df.reset_index ()  

For speed Edit If you do not really care about column naming

  group = df.groupby ( 'Date'). Index df = pd.DataFrame (data = groups.values ​​(), index = groups.keys ()). Reset_index ()  

Output

  Date Val1 Val2 Val3 Val4 0 10-12-2014 3.45 3.67 4 5 1 10- 13-2014 6.00 8.90 NAN NAN  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -