[Solved] Tuples conversion into JSON with python [closed]

Assuming you want a list of dicts as the output of the json with each dict be the form in your question: The following one liner will put it into the data structure your looking for with each tuple generating it’s own complete structure: [{‘name’:i[0], ‘children’: [{‘name’: i[1], ‘value’: i[2]}]} for i in tuples] But … Read more

[Solved] Python returns “SyntaxError: invalid syntax sys module” [closed]

import pandas as pd sys.path.insert(0, “/usr/lib/python2.7/site-packages”) This line contains two statements. Split them into two lines: import pandas as pd sys.path.insert(0, “/usr/lib/python2.7/site-packages”) Or, if they must be in one line, separate them with semicolon (highly not recomended!!!): import pandas as pd; sys.path.insert(0, “/usr/lib/python2.7/site-packages”) 1 solved Python returns “SyntaxError: invalid syntax sys module” [closed]

[Solved] converting a text file to csv file with out index

Try this: from io import StringIO csvtext = StringIO(“””1 1 1 1 1 2 3 4 4 4 4″””) data = pd.read_csv(csvtext, sep=’\n’, header=None) data.to_csv(‘out.csv’, index=False, header=False) #on windows !type out.csv Output: 1 1 1 1 1 2 3 4 4 4 4 2 solved converting a text file to csv file with out index

[Solved] How to compute the ratio of Recovered Cases to Confirmed Cases for each nation using pandas in 8 lines [closed]

Computing the ratio Since there are multiple regions in a country, there are duplicated values in the Country_Region column. Therefore, I use groupby to sum the total cases of a nation. ratio = df.groupby(“Country_Region”)[[“Recovered”, “Confirmed”]].sum() ratio[“Ratio”] = ratio[“Recovered”] / ratio[“Confirmed”] Let’s get the first five nations. >>> ratio.head() Recovered Confirmed Ratio Country_Region Afghanistan 41727 52513 … Read more

[Solved] Sorting Pandas data by hour of the day

after converting to datetime pd.to_datetime(df[‘date’]) you can create a separate column with the hour in it, e.g. df[‘Hour’] = df.date.dt.hour and then sort by it df.sort_values(‘Hour’) EDIT: As you want to sort by time you can instead of using hour, put the timestamp part into a ‘time’ column. In order to get times between 9 … Read more

[Solved] how to save sql query result to csv in pandas

You can try following code: import pandas as pd df1 = pd.read_csv(“Insert file path”) df2 = pd.read_csv(“Insert file path”) df1[‘Date’] = pd.to_datetime(df1[‘Date’] ,errors=”coerce”,format=”%Y-%m-%d”) df2[‘Date’] = pd.to_datetime(df2[‘Date’] ,errors=”coerce”,format=”%Y-%m-%d”) df = df1.merge(df2,how=’inner’, on =’Date’) df.to_csv(‘data.csv’,index=False) This should solve your problem. 4 solved how to save sql query result to csv in pandas

[Solved] how can I loop to get the list named vlist?

Check this out. List Comprehension import numpy as np import pandas as pd df = pd.read_csv(‘census.csv’) data = [‘SUMLEV’,’STNAME’, ‘CTYNAME’, ‘CENSUS2010POP’] df=df[data] adf = df[df[‘SUMLEV’]==50] adf.set_index(‘STNAME’, inplace=True) states = np.array(adf.index.unique()) vlist=[adf.loc[states[i]][‘CENSUS2010POP’].sort_values(ascending =False).head(3).sum() for i in range(0,7)] 3 solved how can I loop to get the list named vlist?

[Solved] how to calculation cost time [closed]

I think I understand what you’re asking. You just want to have a new dataframe that calculates the time difference between the three different entries for each unique order id? So, I start by creating the dataframe: data = [ [11238,3943,201805030759165986,’新建订单’,20180503075916,’2018/5/3 07:59:16′,’2018/5/3 07:59:16′], [11239,3943,201805030759165986,’新建订单’,20180503082115,’2018/5/3 08:21:15′,’2018/5/3 08:21:15′], [11240,3943,201805030759165986,’新建订单’,20180503083204,’2018/5/3 08:32:04′,’2018/5/3 08:32:04′], [11241,3941,201805030856445991,’新建订单’,20180503085644,’2018/5/3 08:56:02′,’2018/5/3 08:56:44′], [11242,3941,201805022232081084,’初审成功’,20180503085802,’2018/5/3 08:58:02′,’2018/5/3 08:58:02′], … Read more