pandas

DataFrame: if value in a cell, copy value to cells below it

天大地大妈咪最大 提交于 2021-02-10 14:47:39
问题 I'm working on a stock analysis program and need to find 'SPLIT' amounts from the 'UNP_action', and then copy the corresponding 'UNP_action_amount' to rows above it only. I'm able to do this in a complicated way via loops, but I'm wondering if there's a more efficient way to do this within Pandas. Current: Date UNP_Adj_Close UNP_action UNP_action_amount 2008-05-23 31.83157 2008-05-27 33.032365 2008-05-28 32.965423 2008-05-29 33.61812 SPLIT 0.5 2008-05-30 34.438176 Desired: Date UNP_Adj_Close

How to create a summary table to use in model pandas python

不打扰是莪最后的温柔 提交于 2021-02-10 14:43:43
问题 I have the following dataset # Import pandas library import pandas as pd import numpy as np # initialize list of lists data = [['tom', 10,1], ['tom', 15,5], ['tom', 14,1], ['tom', 15,4], ['tom', 18,1], ['tom', 15,6], ['tom', 17,3] , ['tom', 14,7], ['tom',16 ,6], ['tom', 22,2],['matt', 10,1], ['matt', 15,5], ['matt', 14,1], ['matt', 15,4], ['matt', 18,1], ['matt', 15,6], ['matt', 17,3] , ['matt', 14,7], ['matt',16 ,6], ['matt', 22,2]] # Create the pandas DataFrame df = pd.DataFrame(data,

Rolling Average in Pandas

☆樱花仙子☆ 提交于 2021-02-10 14:43:41
问题 I have a dataframe with 2 columns - Date and Price. The data is sorted with newest date first (23 Jan in first row, 22 Jan in second row and so on). Date Price 23 Jan 100 22 Jan 95 21 Jan 90 . . . I want to calculate 2 days rolling average price for this time series data. I am using this: df.rolling(2).mean() What this does is, it assigns NaN to the first row (23 Jan) and then for the second row gives the output as the mean of prices on 23 Jan and 22 Jan. This is not useful as 22 Jan average

Rolling Average in Pandas

▼魔方 西西 提交于 2021-02-10 14:42:11
问题 I have a dataframe with 2 columns - Date and Price. The data is sorted with newest date first (23 Jan in first row, 22 Jan in second row and so on). Date Price 23 Jan 100 22 Jan 95 21 Jan 90 . . . I want to calculate 2 days rolling average price for this time series data. I am using this: df.rolling(2).mean() What this does is, it assigns NaN to the first row (23 Jan) and then for the second row gives the output as the mean of prices on 23 Jan and 22 Jan. This is not useful as 22 Jan average

Error com_error: (-2147221005, 'Invalid class string', None, None) while writing dataframe into excel binary notebook

纵然是瞬间 提交于 2021-02-10 14:42:11
问题 I am trying to write a data frame into excel sheet(xlsb), which is having formulas, using xlwing library: app = xw.App() book = xw.Book('ABC.xlsb') sheet = book.sheets('SL Dump') sheet.range('A1').values=final_merge_dataframe After running the above code, I am getting this error: com_error: (-2147221005, 'Invalid class string', None, None) 来源: https://stackoverflow.com/questions/57060655/error-com-error-2147221005-invalid-class-string-none-none-while-writing

How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

断了今生、忘了曾经 提交于 2021-02-10 14:41:53
问题 I have a dictionary containing results of a user's social media activity, the values of the dictionary are in a list containing Timestamp objects and I want to convert it to time objects (datetime.time()) here is the dictionary {{'Instagram':[Timestamp('2020-08-23 04:16:05.12456'), Timestamp('2020-08-23 04:17:02.88754'), Timestamp('2020-08-23 05:20:21.43215'), Timestamp('2020-08-23 06:21:19.63441'), Timestamp('2020-08-23 08:23:15.76421')]}, {'Twitter':[Timestamp('2020-08-23 05:19:12.21245'),

pandas mapping list to dict items for new column

久未见 提交于 2021-02-10 14:41:05
问题 i have df like: col_A [1,2,3] [2,3] [1,3] and dict like: dd = {1: "Soccer", 2: "Cricket", 3: "Hockey"} how can i create a new column col_B like: col_A col_B [1,2,3] ["Soccer", "Cricket", "Hockey"] [2,3] ["Cricket", "Hockey"] [1,3] ["Soccer", "Hockey"] tried something like: df['sports'] = df['col_A'].map(dd) got error: TypeError: unhashable type: 'list' 回答1: You can use list comprehension with if for filter out not matched values: df['sports'] = df['col_A'].map(lambda x: [dd[y] for y in x if y

Error com_error: (-2147221005, 'Invalid class string', None, None) while writing dataframe into excel binary notebook

别来无恙 提交于 2021-02-10 14:37:17
问题 I am trying to write a data frame into excel sheet(xlsb), which is having formulas, using xlwing library: app = xw.App() book = xw.Book('ABC.xlsb') sheet = book.sheets('SL Dump') sheet.range('A1').values=final_merge_dataframe After running the above code, I am getting this error: com_error: (-2147221005, 'Invalid class string', None, None) 来源: https://stackoverflow.com/questions/57060655/error-com-error-2147221005-invalid-class-string-none-none-while-writing

Expand nested list of dictionaries in a pandas dataframe column

巧了我就是萌 提交于 2021-02-10 14:36:29
问题 I have this dataframe called "leads" I got from saving the output of an SFDC SOQL into a dataframe. I have been trying to expand column "Leads__r.record" Company Month Amount Leads__r.done Leads__r.record Leads__r.totalSize 0 A1 September 500000 True [{u'Id': u'Q500, u'Company': u'... 1.0 1 B1 December 16200 True [{u'Id': u'Q600', u'Company': u'... 1.0 2 C1 December 35000 True [{u'Id': u'Q700', u'Company': u'... 1.0 3 D1 December 16200 True [{u'Id': u'Q800', u'Company': u'... 1.0 4 E1

create seaborn plot with pandas of matplotlib

本小妞迷上赌 提交于 2021-02-10 14:35:53
问题 I'm struggling to (re)create a seaborn plot with pandas or matplotlib. DataFrame: wage = pd.melt(pd.read_html('https://en.wikipedia.org/wiki/List_of_countries_by_average_wage')[8].iloc[:5], id_vars=['Country'],var_name='Year', value_name='Wage') print(wage.sample(n=7)) Result: Country Year Wage 29 Netherlands 2013 52808 38 United States 2015 60692 4 Netherlands 2000 47596 9 Netherlands 2005 49939 23 United States 2012 58669 46 Switzerland 2017 62283 12 Iceland 2010 44558 The plot using