Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - Write a Pandas Series out of certain csv file columns

Let's say I am trying to make a (Pandas) Series from 2 csv files that look like this:

A.csv (no column names)

    28/05/2020 22:15,0.88
    28/05/2020 22:30,0.85
    28/05/2020 22:45,0.97


B.csv 

    28/05/2020 22:15,0.05
    28/05/2020 22:30,0.12
    28/05/2020 22:45,0.18

The array would have to look something like this:

[0.88,0.85,0.97,..., 0.05,0.12,0.18,..., timestamp(28/05/2020 22:15)]

So far i have come up with this to write the array:

datarow= pd.DataFrame
for k, v in datarow():


    if k < 96:
           data = pd.concat([A, pd.Series([v], index=[k])])
           data.append(pd.Series(v,0), ignore_index=True)
              
    elif k < 2*96:
           data = pd.concat([B, pd.Series([v], index=[96-k])])
           data.append(pd.Series(v,0), ignore_index=True)
            
    else:
           timestamp = v 

When printed, it shows an empty array. What are possible solutions?

question from:https://stackoverflow.com/questions/65944425/write-a-pandas-series-out-of-certain-csv-file-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First create a Series out of the desired column as follows:

series = dataframe.column

Extract the value of the first timestamp of the timestamps column:

timestamp = dataframe.timestamps.iloc[0]

Set the last field of your series to be the timestamp you just extracted:

series.iloc[-1] = timestamp

Convert your series to a list:

series_as_list = series.tolist()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...