Found 26504 Articles for Server Side Programming

Program to count number of on lights flipped by n people in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:17:19

197 Views

Suppose we have a number n, consider there are n toggle switches in a room and there are n people present in that room, they flip switches as follows −Person 1 comes and flips all switches.Person 2 comes and flips switches that are multiples of 2: 2, 4, 6, ...Person i comes and flips switches that are multiples of i. and so on.We have to find the number of switches that will be in on position finally.So, if the input is like n = 5, then the output will be 2, as initially bulbs are [0, 0, 0, 0, 0].After ... Read More

Python Pandas - Create a TimeDeltaIndex object

AmitDiwan
Updated on 19-Oct-2021 10:24:30

726 Views

To create a TimeDeltaIndex object, use the pandas.TimedeltaIndex() method.At first, import the required libraries −import pandas as pdCreate a TimeDeltaIndex object. We have set the timedelta-like data using the 'data' parameter as well −tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Display TimedeltaIndex −print("TimedeltaIndex...", tdIndex)Return a dataframe of the components of TimeDeltas −print("The Dataframe of the components of TimeDeltas...", tdIndex.components)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the 'data' parameter as well tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min ... Read More

Program to find lexicographically smallest lowercase string of length k and distance n in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:13:24

356 Views

Suppose we have two numbers n and k. We have to find the lexicographically smallest lowercase string of size k and distance n. The distance is the sum of the letter number in alphabet. For example, 'a' has letter number 1, 'b' has 2, 'y' has 25, 'z' has 26 as so on.So, if the input is like n = 15 k = 3, then the output will be "aam", because "aam" is the lexicographically smallest string of length 3 whose distance is 1 + 1 + 13 = 15.To solve this, we will follow these steps −dist := an ... Read More

Python Pandas - Create a DataFrame from DateTimeIndex but override the name of the resulting column

AmitDiwan
Updated on 19-Oct-2021 10:11:27

891 Views

To create a DataFrame from DateTimeIndex, use the datetimeindex.to_frame(). We have set the name parameter to override the name of the resulting column.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) The original index isn't set in the returned DataFrame using the 'False' parameter. To override the name of the resulting column, we have used the 'name' parameter −print("DateTimeIndex to DataFrame...", datetimeindex.to_frame(index=False, name = 'DateTimeData'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 ... Read More

Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index

AmitDiwan
Updated on 19-Oct-2021 10:10:25

3K+ Views

To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex.to_frame() method. Set the parameter index to False to ignore the index.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) Create a DataFrame from DateTimeIndex. The original index isn't set in the returned DataFrame using the 'False' parameter −print("DateTimeIndex to DataFrame...", datetimeindex.to_frame(index=False))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as S i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 ... Read More

Python Pandas - Convert the DateTimeIndex to Series excluding the TimeZone

AmitDiwan
Updated on 19-Oct-2021 10:09:06

166 Views

To convert the DateTimeIndex to Series excluding the TimeZone, use the datetimeindex.tz_convert(None).to_series(). The tz.convert(None) is used to exclude the timezone.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) Convert DateTimeIndex to Series. Use the "tz_convert()" and set it to "None" to exclude the TimeZone −print("DateTimeIndex to series excluding the TimeZone...", datetimeindex.tz_convert(None).to_series())ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as S i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, ... Read More

Python Pandas - Convert the DateTimeIndex to Series

AmitDiwan
Updated on 19-Oct-2021 10:07:55

1K+ Views

To convert the DateTimeIndex to Series, use the DateTimeIndex.to_series() method.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds. The timezone is Australia/Adelaide −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Convert DateTimeIndex to Series −print("DateTimeIndex to series...", datetimeindex.to_series())ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as S i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...", datetimeindex.freq) # Convert DateTimeIndex to Series print("DateTimeIndex to ... Read More

Program to find length of the longest path in an n-ary tree in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:11:56

435 Views

Suppose we have an edge list where each items are holding (u, v) represents u is parent of v. We have to find the length of the longest path in the tree. The path length is 1 + number of nodes in that path.So, if the input is likethen the output will be 5, because the path is [1, 4, 5, 7], there are 4 nodes in total, so path length is 1 + 4 = 5.To solve this, we will follow these steps −g := adjacency list of the graph from given edge listd := a new mapDefine a ... Read More

Python Pandas - Return DatetimeIndex as object ndarray of datetime.datetime objects

AmitDiwan
Updated on 19-Oct-2021 10:06:45

478 Views

To return DatetimeIndex as object ndarray of datetime.datetime objects, use the datetimeindex.to_pydatetime() method.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as Y i.e. year −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Return DatetimeIndex as object ndarray −print("Return DatetimeIndex as object ndarray of datetime.datetime objects...", datetimeindex.to_pydatetime())ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as Y i.e. year datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...", datetimeindex.freq) # Return DatetimeIndex as ... Read More

Python Pandas - Calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq

AmitDiwan
Updated on 19-Oct-2021 10:05:15

230 Views

To calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq, use the datetimeindex.to_perioddelta() method. Set the frequency using the freq parameter.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 7 and frequency as Y i.e. year −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Calculate TimedeltaArray of difference between index values and index converted to PeriodArray. We have set the Period frequency using the "freq" parameter with value 'M' −print("Convert DateTimeIndex to PeriodDelta...", datetimeindex.to_perioddelta(freq='M'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 7 and ... Read More

Advertisements