pandas

Renaming the column names of pandas dataframe is not working as expected - python

让人想犯罪 __ 提交于 2021-02-18 06:43:11
问题 I am having below pandas dataframe df . I am trying to rename the column names but it not working as expected. Code: mapping = {df.columns[0]:'Date', df.columns[1]: 'A', df.columns[2]:'B', df.columns[3]: 'C',df.columns[4]:'D', df.columns[5]: 'E',df.columns[6]:'F', df.columns[7]: 'G',df.columns[8]:'H', df.columns[9]: 'J'} df.rename(columns=mapping) Output of df.columns : MultiIndex(levels=[['A Index', 'B Index', 'C Index', 'D Index', 'E Index', 'F Index', 'G Index', 'H Index', 'I Index', 'J

Print OLS regression summary to text file

╄→гoц情女王★ 提交于 2021-02-18 05:56:41
问题 I am running OLS regression using pandas.stats.api.ols using a groupby with the following code: from pandas.stats.api import ols df=pd.read_csv(r'F:\file.csv') result=df.groupby(['FID']).apply(lambda d: ols(y=d.loc[:, 'MEAN'], x=d.loc[:, ['Accum_Prcp', 'Accum_HDD']])) for i in result: x=pd.DataFrame({'FID':i.index, 'delete':i.values}) frame = pd.concat([x,DataFrame(x['delete'].tolist())], axis=1, join='outer') del frame['delete'] print frame but this returns the error: AttributeError: 'OLS'

How to get python graph output into html webpage directly

半城伤御伤魂 提交于 2021-02-18 05:25:46
问题 I am using different libraries like pandas and numpy for generating a dataframe, which eventually generate a graph. Now, I need to show this graph into a simple webpage which is in HTML. Note: I am also willing to take 2-3 input from user in HTML page then pass that data to my python file. Afterwards, python file generates a graph based on given data(from HTML page) and I need to pass this graph to an HTML page. df[[main_data]].plot() Here, main_data is variable whose value is coming from

Pandas: Change a specific column name in dataframe having multilevel columns

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-18 05:17:42
问题 I want to find the way change name of specific column in a multilevel dataframe. With this data: data = { ('A', '1', 'I'): [1, 2, 3, 4, 5], ('B', '2', 'II'): [1, 2, 3, 4, 5], ('C', '3', 'I'): [1, 2, 3, 4, 5], ('D', '4', 'II'): [1, 2, 3, 4, 5], ('E', '5', 'III'): [1, 2, 3, 4, 5], } dataDF = pd.DataFrame(data) This code not working: dataDF.rename(columns = {('A', '1', 'I'):('Z', '100', 'Z')}, inplace=True) Result: A B C D E 1 2 3 4 5 I II I II III 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4

How to keep original index of a DataFrame after groupby 2 columns?

只谈情不闲聊 提交于 2021-02-18 04:54:44
问题 Is there any way I can retain the original index of my large dataframe after I perform a groupby? The reason I need to this is because I need to do an inner merge back to my original df (after my groupby) to regain those lost columns. And the index value is the only 'unique' column to perform the merge back into. Does anyone know how I can achieve this? My DataFrame is quite large. My groupby looks like this: df.groupby(['col1', 'col2']).agg({'col3': 'count'}).reset_index() This drops my

How to keep original index of a DataFrame after groupby 2 columns?

别说谁变了你拦得住时间么 提交于 2021-02-18 04:53:17
问题 Is there any way I can retain the original index of my large dataframe after I perform a groupby? The reason I need to this is because I need to do an inner merge back to my original df (after my groupby) to regain those lost columns. And the index value is the only 'unique' column to perform the merge back into. Does anyone know how I can achieve this? My DataFrame is quite large. My groupby looks like this: df.groupby(['col1', 'col2']).agg({'col3': 'count'}).reset_index() This drops my

Create a column which increments value for changes in another row

百般思念 提交于 2021-02-17 23:22:38
问题 I have a dataframe with two columns as below: Var1Var2 a 28 b 28 d 28 f 29 f 29 e 30 b 30 m 30 l 30 u 31 t 31 t 31 I'd like to create a third column with values which increases by one for every change in value of another column. Var1Var2Var3 a 28 1 b 28 1 d 28 1 f 29 2 f 29 2 e 30 3 b 30 3 m 30 3 l 30 3 u 31 4 t 31 4 t 31 4 How would I go about doing this? 回答1: Using category df.Var2.astype('category').cat.codes.add(1) Out[525]: 0 1 1 1 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 dtype: int8

Create a column which increments value for changes in another row

一曲冷凌霜 提交于 2021-02-17 23:14:35
问题 I have a dataframe with two columns as below: Var1Var2 a 28 b 28 d 28 f 29 f 29 e 30 b 30 m 30 l 30 u 31 t 31 t 31 I'd like to create a third column with values which increases by one for every change in value of another column. Var1Var2Var3 a 28 1 b 28 1 d 28 1 f 29 2 f 29 2 e 30 3 b 30 3 m 30 3 l 30 3 u 31 4 t 31 4 t 31 4 How would I go about doing this? 回答1: Using category df.Var2.astype('category').cat.codes.add(1) Out[525]: 0 1 1 1 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 dtype: int8

Using map() for columns in a pandas dataframe

吃可爱长大的小学妹 提交于 2021-02-17 21:19:06
问题 I have some columns in my dataframe for which I just want to keep the date part and remove the time part. I have made a list of these columns: list_of_cols_to_change = ['col1','col2','col3','col4'] I have written a function for doing this. It takes a list of columns and applies dt.date to each column in the list. def datefunc(x): for column in x: df[column] = df[column].dt.date I then call this function passing the list as parameter: datefunc(list_of_cols_to_change ) I want to accomplish this

Pretty print a pandas dataframe in VS Code

↘锁芯ラ 提交于 2021-02-17 21:16:35
问题 I'd like to know if it's possible to display a pandas dataframe in VS Code while debugging (first picture) as it is displayed in PyCharm (second picture) ? Thanks for any help. df print in vs code: df print in pycharm: 回答1: As of the January 2021 release of the python extension, you can now view pandas dataframes with the built-in data viewer when debugging native python programs. When the program is halted at a breakpoint, right-click the dataframe variable in the variables list and select