I would like to combine the data of the Pandas data frame for 3 days into one Row.

Asked 6 days ago, Updated 6 days ago, 1 views

Hello. I'm pre-processing the Pandas data frame data, but I'm inquiring because there's a blockage.

What I want to do first is to combine three days of data into one Row per Oper, as shown in . For example, when Oper1's September 1st data ends, you have September 2nd data immediately followed, and then September 3rd data.

After that, I want to make the same type of data frame as the data of Oper2 enters the second Row, but I am inquiring because it is not as well made as I thought. I want to make a 10 x 78 Dataset I didn't know how to handle the index, so it became 1 x 780. I'd appreciate it if you could help me, masters.

df = pd.DataFrame()
for i in range(0,10):
    for j in range(0,3):
        result = data1[j:j+1]
        result = result.reset_index(drop=True)
        df = pd.concat([df,result], ignore_index = True,axis=1)

pandas python

2022-09-20 15:07

1 Answers

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pandas as pd

>>> df = pd.DataFrame({"a":[1,2,3,4,5,6], "b":list("abcdef")})
>>> df
   a  b
0  1  a
1  2  b
2  3  c
3  4  d
4  5  e
5  6  f
>>> df[0::3]
   a  b
0  1  a
3  4  d
>>> df[1::3]
   a  b
1  2  b
4  5  e
>>> df[2::3]
   a  b
2  3  c
5  6  f
>>> pd.concat([df[0::3], df[1::3], df[2::3]], axis=1)
     a    b    a    b    a    b
0  1.0    a  NaN  NaN  NaN  NaN
1  NaN  NaN  2.0    b  NaN  NaN
2  NaN  NaN  NaN  NaN  3.0    c
3  4.0    d  NaN  NaN  NaN  NaN
4  NaN  NaN  5.0    e  NaN  NaN
5  NaN  NaN  NaN  NaN  6.0    f
>>> df[0::3].reset_index(drop=True)
   a  b
0  1  a
1  4  d
>>> df[1::3].reset_index(drop=True)
   a  b
0  2  b
1  5  e
>>> df[2::3].reset_index(drop=True)
   a  b
0  3  c
1  6  f
>>> pd.concat([df[0::3].reset_index(drop=True), df[1::3].reset_index(drop=True), df[2::3].reset_index(drop=True)], axis=1)
   a  b  a  b  a  b
0  1  a  2  b  3  c
1  4  d  5  e  6  f
>>> 


2022-09-20 15:07

If you have any answers or tips


© 2022 pinfo. All rights reserved.