0

Usually, I use a list I simplify the timestamp by finding the difference between two successive values like this:

 x=[ 1552154111, 1552154115, 1552154117, 1552154120, 1552154125 
    ,1552154127, 1552154134, 1552154137]
    List_time = []
    for i in x:
        List_time.append((i + 1) - x[0])
    print(List_time)
[1, 5, 7, 10, 15, 17, 24, 27]

I need to have the same result by using the dataframe, which looks like this:

print(df['Timestamp'])

0         1552154111
1         1552154115
2         1552154117
3         1552154120
4         1552154125
5         1552154127
6         1552154134
7         1552154137

I need to replace the currect timestamp column with the expected difference. I don't know how to do that. It is the first I use a dataframe.

How could I do that please?

2 Answers 2

2

A potential solution that does not involve a df.apply(lambda) loop:

df['Timestamp'] = df['Timestamp'] - df['Timestamp'].iloc[0] + 1
Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve it with :

first_value = df.loc[0]
new_row = df.apply(lambda x: x + 1 - first_value)

first_value represents x[0].

Usually, you can achieve element-wise operations on pandas Series with apply

4 Comments

thank you but, it gives me this error: lambda x: op(x, rvalues)) TypeError: ("Can't convert 'int' object to str implicitly", 'occurred at index Timestamp')
What does first_value return? What is type(first_value)? Maybe df.loc[0] does not access your first line
print(first_value) and print(df['Timestamp'].loc[0]) give me both 1552154111
Is the type of first_value an int? Is it the same for all the entries in the Timestamp column?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.