Home>
#-*-coding: utf-8-*-
import pandas as pd

Read #csv file
df = pd.read_csv ("./ test2.csv")

Extract only #datetime and id columns
df = df [["datetime", "id"]]

#Datatime numeric format can be calculated
df ["new"] = pd.to_datetime (df ["datetime"])

#Speed ​​processing (second speed)
i = 0
a = 1
while i<= 8 and a<8:

    If #id is the same, ask for speed per second
    if df.iloc [i] [1] == df.iloc [a] [1]:

        #When the minutes are the same, only the number of seconds is calculated
        if df ["new"] [i] .minute == df ["new"] [a] .minute:
            df ["speed"] = 50 // (df ["new"] [a] .second-df ["new"] [i] .second)
            print (50 // (df ["new"] [a] .second-df ["new"] [i] .second))

        #Calculate by changing minutes to seconds
        else:
            df ["speed"] = 50 // ((df ["new"] [a] .minute-df ["new"] [i] .minute) * 60 + (df ["new"] [a]. second-df ["new"] [i] .second))

    #elif df.iloc [i] [1]! = df.iloc [a] [1]:
        #df ["speed"] = "nothing"

    i + = 1
    a + = 1

#Check output format of csv file
print (df)

#Write csv file (write the value of speed per second to csv file)
df.to_csv ('test2.csv')
Question

I am working on writing csv files using python pandas. Is it possible to use pandas to enter different values ​​for specific lines?
If possible, I would be grateful if you could tell me a specific method such as a reference site

csv file to import

, datetime, id
0,2019-02-21 17: 15: 14.500000,9
1,2019-02-21 17: 15: 15.700000,9
2,2019-02-21 17: 15: 33.300000,117
3,2019-02-21 17: 16: 11.600000,213
4,2019-02-21 17: 16: 12.700000,213
5,2019-02-21 17: 16: 12.700000,193
6,2019-02-21 17: 19: 10.300000,533
7,2019-02-21 17: 19: 11.400000,533
8,2019-02-21 17: 20: 14.800000,681

  • Answer # 1

    I imagine that this is probably what I want to do, but how about it?

    #-*-coding: utf-8-*-
    import pandas as pd
    Read #csv file
    df = pd.read_csv ("./ test2.csv", index_col = 0, parse_dates = ['datetime'])
    for i in df.index [:-1]:
        If #id is the same, ask for speed per second
        if df.loc [i, 'id'] == df.loc [i + 1, 'id']:
            df.loc [i, 'speed'] = 50/(df.loc [i + 1, "datetime"]-df.loc [i, "datetime"]). total_seconds ()
    #Check output format of csv file
    print (df)
    # datetime id speed
    # 0 2019-02-21 17: 15: 14.500 9 41.666667
    # 1 2019-02-21 17: 15: 15.700 9 NaN
    # 2 2019-02-21 17: 15: 33.300 117 NaN
    # 3 2019-02-21 17: 16: 11.600 213 45.454545
    # 4 2019-02-21 17: 16: 12.700 213 NaN
    # 5 2019-02-21 17: 16: 12.700 193 NaN
    # 6 2019-02-21 17: 19: 10.300 533 45.454545
    # 7 2019-02-21 17: 19: 11.400 533 NaN
    # 8 2019-02-21 17: 20: 14.800 681 NaN

    A major fix is ​​

    Addindex_colandparse_datesas parameters ofread_csv ()when reading CSV

    You don't have to bother to calculate the time difference every minute and every second, just subtract and get the seconds withtotal_seconds ()

    Assign a value to a specific cell of DataFrame withdf.loc [row index value, column name of column] = value

    (The same is true for cell values)
    However, with the above method, even if the ID value is the same, the speed is not calculated unless the rows are continuous, sogroupby ('id')I think it ’s better to calculate

    import pandas as pd
    Read #csv file
    df = pd.read_csv ("./ test2.csv", index_col = 0, parse_dates = ['datetime'])
    If #id is the same, ask for speed per second
    df ['speed'] = df.groupby ('id') ['datetime']. apply (lambda d: 5/d.diff (). shift (-1) .dt.total_seconds ())
    #Check output format of csv file
    print (df)
    # datetime id speed
    # 0 2019-02-21 17: 15: 14.500 9 41.666667
    # 1 2019-02-21 17: 15: 15.700 9 NaN
    # 2 2019-02-21 17: 15: 33.300 117 NaN
    # 3 2019-02-21 17: 16: 11.600 213 45.454545
    # 4 2019-02-21 17: 16: 12.700 213 NaN
    # 5 2019-02-21 17: 16: 12.700 193 NaN
    # 6 2019-02-21 17: 19: 10.300 533 45.454545
    # 7 2019-02-21 17: 19: 11.400 533 NaN
    # 8 2019-02-21 17: 20: 14.800 681 NaN

  • Answer # 2

    As it is, if you go around with "csv file python pandas", you will get a useful page