pandas

Python, Merging rows with same value in one column

半世苍凉 提交于 2021-02-20 04:27:26
问题 My dataframe looks like this: ID Class 0 9 1 8 1 6 2 6 2 2 3 15 3 1 3 8 What I would like to do is merging rows with same ID value in a way below: ID Class1 Class2 Class3 0 9 1 8 6 2 6 2 3 15 1 8 So for each ID which exists more than once, I want to create new column(s) and move values from rows to those columns. What is the fastest way to do this? I tried using groupby but it didn't give me appriopate results. 回答1: Use set_index with cumcount for new columns, reshape by unstack and last

Delete rows from pandas dataframe if all its columns have empty string

北城余情 提交于 2021-02-20 04:22:31
问题 I have a dataframe as follows Name Age 0 Tom 20 1 nick 21 2 3 krish 19 4 jack 18 5 6 jill 26 7 nick Desired output is Name Age 0 Tom 20 1 nick 21 3 krish 19 4 jack 18 6 jill 26 7 nick The index should not be changed and if possible would be nice if I don't have to convert empty strings to NaN. It should be removed only if all the columns have '' empty strings 回答1: You can do: # df.eq('') compare every cell of `df` to `''` # .all(1) or .all(axis=1) checks if all cells on rows are True # ~ is

How to set a cell not column or row in a dataframe with color?

梦想与她 提交于 2021-02-20 04:12:54
问题 I have a dataframe with table style that I created : tableyy = final.style.set_table_attributes('border="" class = "dataframe table table-hover table-bordered"').set_precision(10).render() I have go through this Coloring Cells in Pandas , Conditionally change background color of specific cells, Conditionally format Python pandas cell, and Colour cells in pandas dataframe, I still not able to set a cell with color not the whole dataframe without any condition. Anyone have any ideas, I try this

API Call- Statistics Sweden

我与影子孤独终老i 提交于 2021-02-20 04:12:33
问题 I am trying to access the following API URL and convert it into a Pandas DataFrame: http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningR1860 My goal is to read this JSON file into a pandas data frame and display years as index and the population of Sweden as values. These are the values for each year: - 2012: 9 555 893 - 2013: 9 644 864 - 2014: 9 747 355 My solution looks like this so far: import pandas as pd url = 'http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE

How to set a cell not column or row in a dataframe with color?

霸气de小男生 提交于 2021-02-20 04:11:13
问题 I have a dataframe with table style that I created : tableyy = final.style.set_table_attributes('border="" class = "dataframe table table-hover table-bordered"').set_precision(10).render() I have go through this Coloring Cells in Pandas , Conditionally change background color of specific cells, Conditionally format Python pandas cell, and Colour cells in pandas dataframe, I still not able to set a cell with color not the whole dataframe without any condition. Anyone have any ideas, I try this

issue with encoding when importing json into Postgres

廉价感情. 提交于 2021-02-20 04:09:50
问题 I'm using pandas, and exporting data as json like this: import pandas as pd df = pd.DataFrame({'a': ['Têst']}) df.to_json(orient='records', lines=True) > u'{"a":"T\\u00east"}' This makes sense since we have a Unicode character 00ea prefixed with \u and it is escaped with \ when converted to JSON But then I import the JSON strings into Postgres with COPY buffer = cStringIO.StringIO() buffer.write(df.to_json(orient='records', lines=True)) buffer.seek(0) with connection.cursor() as cursor:

How to set a cell not column or row in a dataframe with color?

一笑奈何 提交于 2021-02-20 04:09:11
问题 I have a dataframe with table style that I created : tableyy = final.style.set_table_attributes('border="" class = "dataframe table table-hover table-bordered"').set_precision(10).render() I have go through this Coloring Cells in Pandas , Conditionally change background color of specific cells, Conditionally format Python pandas cell, and Colour cells in pandas dataframe, I still not able to set a cell with color not the whole dataframe without any condition. Anyone have any ideas, I try this

How to save a list of dataframes to csv

江枫思渺然 提交于 2021-02-20 03:51:46
问题 I have a list of data frames which I reshuffle and then I want to save the output as a csv. To do this I'm trying to append this list to an empty data frame: l1=[year1, year2,..., year30] shuffle (l1) columns=['year', 'day', 'tmin', 'tmax', 'pcp'] index=np.arange(10957) df2=pd.DataFrame(columns=columns, index=index) l1.append(df2) This result in an empty data frames with a bunch of Nans. I don't necessarily need to append my reshuffled list to a dataframe, I just need to save it as a csv, and

flatten_json recursive flattening function for lists

☆樱花仙子☆ 提交于 2021-02-20 03:09:27
问题 I want to flatten the following JSON at each level and create a pandas dataframe per level, Im using flatten_json to do that but for that I need to loop through each level which creates multiple nested for loops: { "metadata": { "name": "abc", "time": "2020-04-01" }, "data": [ { "identifiers": [ { "type": "abc", "scheme": "def", "value": "123" }, { "type": "abc", "scheme": "def", "value": "123" } ], "name": "qwer", "type": "abd", "level1": [ { "identifiers": [ { "type": "abc", "scheme": "def"

flatten_json recursive flattening function for lists

こ雲淡風輕ζ 提交于 2021-02-20 03:08:13
问题 I want to flatten the following JSON at each level and create a pandas dataframe per level, Im using flatten_json to do that but for that I need to loop through each level which creates multiple nested for loops: { "metadata": { "name": "abc", "time": "2020-04-01" }, "data": [ { "identifiers": [ { "type": "abc", "scheme": "def", "value": "123" }, { "type": "abc", "scheme": "def", "value": "123" } ], "name": "qwer", "type": "abd", "level1": [ { "identifiers": [ { "type": "abc", "scheme": "def"