Home > Net >  How to read the data from txt to python pandas if the data looks compacted by date?
How to read the data from txt to python pandas if the data looks compacted by date?

Time:10-30

I received a text file, which looks like

Time Purchasing_item
20120701
00:00 Apple pie
00:01 Coca-cola
20120702
00:00 Pepsi
00:01 Fish-ball

However, I want when I load the data into pandas, I want the data to look like

Added one column myself, but don't want to duplicate the date so many times beforehand.

Date     Time  Purchasing_item

20120701 00:00 Apple pie
20120701 00:01 Coca-cola
20120702 00:00 Pepsi
20120702 00:01 Fish-ball

Or... Make 20120701 00:00 as a timestamp

Time   Purchasing_item

20120701 00:00 Apple pie
20120701 00:01 Coca-cola
20120702 00:00 Pepsi
20120702 00:01 Fish-ball

Could ask, how could I make one of the versions without duplicating the date many times manually?

Many thanks for your answer.

CodePudding user response:

Here is one way to do it:

df = pd.read_table('Data/file.txt', names=['DateTime'], skiprows=1)

df[['DateTime', 'Purchasing_item']] = df['DateTime'].str.split(n=1, expand=True)

for i in df.index:
    if df['Purchasing_item'].isna()[i] == True:
        df.iloc[i 1, 0] = df.iloc[i, 0]   ' '   df.iloc[i 1, 0]
        df.iloc[i 2, 0] = df.iloc[i, 0]   ' '   df.iloc[i 2, 0]

df.dropna(inplace=True)

Output:

   DateTime           Purchasing_item
1  20120701 00:00           Apple pie
2  20120701 00:01           Coca-cola
4  20120702 00:00               Pepsi
5  20120702 00:01           Fish-ball
  • Related