pandas

Forward slash in json file from pandas dataframe

為{幸葍}努か 提交于 2021-02-18 22:11:26
问题 I'm a complete newbie to json, any help is appreciated. I'm trying to convert a dataframe to a json file. import pandas as pd df = pd.DataFrame({ 'A' : [1., 2.5], 'B' : ['img/blue.png', 'img/red.png']}) print df Output is A B 0 1.0 img/blue.png 1 2.5 img/red.png I would like to make a json file that looks like this: '[1.0,"img/blue.png"],[2.5,"img/red.png"]' However, when I use the following out = df.to_json(orient='values')[1:-1] print out I get this instead '[1.0,"img\\/blue.png"],[2.5,"img

subtract current time from pandas date column

最后都变了- 提交于 2021-02-18 22:11:06
问题 I have a pandas data frame like x = pd.DataFrame(['05/06/2015 00:00', '22/06/2015 00:00', None], columns=['myDate']) I want to find out the number of days between the dates in the myDate column and the current date. How can I do this? I tried the below without much success pd.to_datetime(x['myDate']) - pd.datetime.now().date() 回答1: the following works for me: In [9]: df = pd.DataFrame(['05/06/2015 00:00', '22/06/2015 00:00', None], columns=['myDate']) df['myDate']= pd.to_datetime(df['myDate']

Convert freq string to DateOffset in pandas

可紊 提交于 2021-02-18 22:10:31
问题 In pandas documentation one can read "Under the hood, these frequency strings are being translated into an instance of pandas DateOffset" when speaking of freq string such as "W" or "W-SUN". Then, how can I get an instance of a DateOffset given a string ? Ultimately want to configure my program with frequency as string (say "W-SUN"), but internally want to do something like offset = Week(weekday=0) if d1-3*offset<d2: pass but defining offset from string. Thanks 回答1: You could use the to

Pandas: fill one column with count of # of obs between occurrences in a 2nd column

拈花ヽ惹草 提交于 2021-02-18 21:57:21
问题 Say I have the following DataFrame which has a 0/1 entry depending on whether something happened/didn't happen within a certain month. Y = [0,0,1,1,0,0,0,0,1,1,1] X = pd.date_range(start = "2010", freq = "MS", periods = len(Y)) df = pd.DataFrame({'R': Y},index = X) R 2010-01-01 0 2010-02-01 0 2010-03-01 1 2010-04-01 1 2010-05-01 0 2010-06-01 0 2010-07-01 0 2010-08-01 0 2010-09-01 1 2010-10-01 1 2010-11-01 1 What I want is to create a 2nd column that lists the # of months until the next

Pandas: fill one column with count of # of obs between occurrences in a 2nd column

时光毁灭记忆、已成空白 提交于 2021-02-18 21:56:35
问题 Say I have the following DataFrame which has a 0/1 entry depending on whether something happened/didn't happen within a certain month. Y = [0,0,1,1,0,0,0,0,1,1,1] X = pd.date_range(start = "2010", freq = "MS", periods = len(Y)) df = pd.DataFrame({'R': Y},index = X) R 2010-01-01 0 2010-02-01 0 2010-03-01 1 2010-04-01 1 2010-05-01 0 2010-06-01 0 2010-07-01 0 2010-08-01 0 2010-09-01 1 2010-10-01 1 2010-11-01 1 What I want is to create a 2nd column that lists the # of months until the next

Pandas: get the min value between 2 dataframe columns

会有一股神秘感。 提交于 2021-02-18 21:11:38
问题 I have 2 columns and I want a 3rd column to be the minimum value between them. My data looks like this: A B 0 2 1 1 2 1 2 2 4 3 2 4 4 3 5 5 3 5 6 3 6 7 3 6 And I want to get a column C in the following way: A B C 0 2 1 1 1 2 1 1 2 2 4 2 3 2 4 2 4 3 5 3 5 3 5 3 6 3 6 3 7 3 6 3 Some helping code: df = pd.DataFrame({'A': [2, 2, 2, 2, 3, 3, 3, 3], 'B': [1, 1, 4, 4, 5, 5, 6, 6]}) Thanks! 回答1: Use df.min(axis=1) df['c'] = df.min(axis=1) df Out[41]: A B c 0 2 1 1 1 2 1 1 2 2 4 2 3 2 4 2 4 3 5 3 5 3

How to add syntax highlight to SQL line magic, cell magic and custom command in jupyter notebook?

北战南征 提交于 2021-02-18 20:15:11
问题 I was searching for ways to highlight SQL codes in jupyter notebook. I was able to highlight SQL cell magic only, but not line magic and custom settings. Case 1 (works) Highlight cell magic (cell startswith %%sql) Ref: adding syntax highlighting to Jupyter notebook cell magic require(['notebook/js/codecell'], function(codecell) { codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ; Jupyter.notebook.events.one('kernel_ready.Kernel', function(){ Jupyter

How to access the last element in a Pandas series?

我的梦境 提交于 2021-02-18 20:13:00
问题 Let us consider the following data frame: import pandas as pd d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]} df=pd.DataFrame(data=d) If I want to access the first element in pandas series df['col1'] , I can simply go df['col1'][0] . But how can I access the last element in this series? I have tried df['col1'][-1] which returns the following error: KeyError: -1L I know that I could go for something like df['col1'][len(df)-1] but why is reverse indexing impossible here? 回答1: For select last value

How to access the last element in a Pandas series?

♀尐吖头ヾ 提交于 2021-02-18 20:12:22
问题 Let us consider the following data frame: import pandas as pd d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]} df=pd.DataFrame(data=d) If I want to access the first element in pandas series df['col1'] , I can simply go df['col1'][0] . But how can I access the last element in this series? I have tried df['col1'][-1] which returns the following error: KeyError: -1L I know that I could go for something like df['col1'][len(df)-1] but why is reverse indexing impossible here? 回答1: For select last value

How to access the last element in a Pandas series?

你。 提交于 2021-02-18 20:10:25
问题 Let us consider the following data frame: import pandas as pd d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]} df=pd.DataFrame(data=d) If I want to access the first element in pandas series df['col1'] , I can simply go df['col1'][0] . But how can I access the last element in this series? I have tried df['col1'][-1] which returns the following error: KeyError: -1L I know that I could go for something like df['col1'][len(df)-1] but why is reverse indexing impossible here? 回答1: For select last value