pandas

Pandas column multi-index to rows

烂漫一生 提交于 2021-02-11 07:09:59
问题 I'm using yfinance to download price history for multiple symbols, which returns a df with multiple indexes. For example: import yfinance as yf df = yf.download(tickers = ['AAPL', 'MSFT'], period = '2d') A similar dataframe could be constructed without yfinance like: import pandas as pd pd.options.display.float_format = '{:.2f}'.format import numpy as np attributes = ['Adj Close', 'Close', 'High', 'Low', 'Open', 'Volume'] symbols = ['AAPL', 'MSFT'] dates = ['2020-07-23', '2020-07-24'] data =

Difference between two dates in Pandas DataFrame

空扰寡人 提交于 2021-02-11 07:09:59
问题 I have many columns in a data frame and I have to find the difference of time in two column named as in_time and out_time and put it in the new column in the same data frame. The format of time is like this 2015-09-25T01:45:34.372Z . I am using Pandas DataFrame. I want to do like this: df.days = df.out_time - df.in_time I have many columns and I have to increase 1 more column in it named days and put the differences there. 回答1: You need to convert the strings to datetime dtype, you can then

Check multiple columns data format and append results to one column in Pandas

只愿长相守 提交于 2021-02-11 07:09:51
问题 Given a toy dataset as follows: id room area situation 0 1 A-102 world under construction 1 2 NaN 24 under construction 2 3 B309 NaN NaN 3 4 C·102 25 under decoration 4 5 E_1089 hello under decoration 5 6 27 NaN under plan 6 7 27 NaN NaN I need to check three columns: room, area, situation based on the following conditions: (1) if room name is not number, alphabet, - ( NaN s are also considered as invalid one), then returns incorrect room name for check column; (2) if area is not number or

Difference between two dates in Pandas DataFrame

情到浓时终转凉″ 提交于 2021-02-11 07:09:18
问题 I have many columns in a data frame and I have to find the difference of time in two column named as in_time and out_time and put it in the new column in the same data frame. The format of time is like this 2015-09-25T01:45:34.372Z . I am using Pandas DataFrame. I want to do like this: df.days = df.out_time - df.in_time I have many columns and I have to increase 1 more column in it named days and put the differences there. 回答1: You need to convert the strings to datetime dtype, you can then

Difference between two dates in Pandas DataFrame

喜夏-厌秋 提交于 2021-02-11 07:07:38
问题 I have many columns in a data frame and I have to find the difference of time in two column named as in_time and out_time and put it in the new column in the same data frame. The format of time is like this 2015-09-25T01:45:34.372Z . I am using Pandas DataFrame. I want to do like this: df.days = df.out_time - df.in_time I have many columns and I have to increase 1 more column in it named days and put the differences there. 回答1: You need to convert the strings to datetime dtype, you can then

how to shift columns in pandas DataFrame dynamically and independently?

狂风中的少年 提交于 2021-02-11 07:06:54
问题 What I wish to achieve is to be able to normalise my values so that they all start on a arbitrary day-zero. Imagine the DataFrame below: df = pd.DataFrame(np.array([[1, 0, 0], [4, 5, 0], [7, 8, 9], [7, 8, 9], [4, 5, 0], [7, 8, 9]]), columns=['a', 'b', 'c'], index = ['1/1/2000', '1/1/2001', '1/1/2002', '1/1/2003', '1/1/2004', '1/1/2005']) Which results in the following: a b c 1/1/2000 1 0 0 1/1/2001 4 5 0 1/1/2002 7 8 9 1/1/2003 7 8 9 1/1/2004 4 5 0 1/1/2005 7 8 9 What I am trying to achieve

perform operation on column of data frame based on condition given to column in another data frame in pandas

ぃ、小莉子 提交于 2021-02-11 07:05:05
问题 I have a data frame df1: df1 = pd.DataFrame([[40, 23, 22, 31], [12, 3, 11,23], [42, 16, 32, 34], [42, 13, 26, 34]], columns=['A', 'B', 'C', 'D']) I have another data frame df2: df2 = pd.DataFrame([["B","<20"],["A",">30"],["C","<40"],["D","<15"]], columns=["Column","Condition"]) Question: Select the data frame df1 based on any of the conditions present in df2. How to do it? Please help. Expected Output example: For B Condition: B_df = pd.DataFrame([3,16,13],columns=["B"]) For C Condition: C_df

perform operation on column of data frame based on condition given to column in another data frame in pandas

旧城冷巷雨未停 提交于 2021-02-11 07:01:25
问题 I have a data frame df1: df1 = pd.DataFrame([[40, 23, 22, 31], [12, 3, 11,23], [42, 16, 32, 34], [42, 13, 26, 34]], columns=['A', 'B', 'C', 'D']) I have another data frame df2: df2 = pd.DataFrame([["B","<20"],["A",">30"],["C","<40"],["D","<15"]], columns=["Column","Condition"]) Question: Select the data frame df1 based on any of the conditions present in df2. How to do it? Please help. Expected Output example: For B Condition: B_df = pd.DataFrame([3,16,13],columns=["B"]) For C Condition: C_df

How to get frequency count of column values for each unique pair of columns in pandas?

旧巷老猫 提交于 2021-02-11 06:52:48
问题 I have a Dataframe that looks like below data = [(datetime.datetime(2021, 2, 10, 7, 49, 7, 118658), u'12.100.90.10', u'100.100.12.1', u'LT_DOWN'), (datetime.datetime(2021, 2, 10, 7, 49, 14, 312273), u'12.100.90.10', u'100.100.12.1', u'LT_UP'), (datetime.datetime(2021, 2, 10, 7, 49, 21, 535932), u'12.100.90.10', u'100.100.12.1', u'LT_UP'), (datetime.datetime(2021, 2, 10, 7, 50, 28, 725961), u'12.100.90.10', u'100.100.12.1', u'PL_DOWN'), (datetime.datetime(2021, 2, 10, 7, 50, 32, 450853), u'10

How to add Arrow annotations with an offset to a bokeh plot with a datetime x-axis

断了今生、忘了曾经 提交于 2021-02-11 06:52:30
问题 I want to draw an arrow or dots when 2 ma cross each other like there will up arrow when short ma cross above long ma etc. but I don't know how to plot when it is datetime. I try to use this code and it just give me errors. #plot short ma and long ma p.line(df['Date'], df['short_ma'], color='red') p.line(df['Date'], df['long_ma'], color='black') p.add_layout(Arrow(end=VeeHead(size=35), line_color="red",x_start=df['Date'], y_start=df['crossabove']+5, x_end=df['Date'], y_end=df['Date'])) #the