简体   繁体   中英

Pandas - Pivot, Stack, Unstack?

I'm wondering how to have the keys be the column headers (Result, IP, time)?

Data in CSV looks like this:

"Result":"Success","IP":"0.0.0.0","time":"2018-08-20T12:00:00.000Z"
"Result":"Failure","IP":"1.1.1.1","time":"2018-08-20T12:01:00.000Z"

I'd like to format it like this:

Result        IP        time
Success    0.0.0.0    2018-08-20T12:00:00.000Z
Failure    1.1.1.1    2018-08-20T12:01:00.000Z

My code so far:

import pandas as pd
file = pd.read_csv("path_to.csv", sep='\n', names = ["Result","IP","time"])
df = pd.DataFrame(file)
print(df.head(1))

You need:

import csv
file = pd.read_csv("foo.csv", sep=',', header=None, quoting=csv.QUOTE_ALL, names=["Result", "IP", "time"])
df = pd.DataFrame(file)
df = df.applymap(lambda x: (''.join(str(x).split(":")[1:]))[1:-1])

Output:

    Result       IP                    time
0  Success  0.0.0.0  2018-08-20T120000.000Z
1  Failure  1.1.1.1  2018-08-20T120100.000Z

Here is a similar approach, also using str.split , but which retains your timezone information by splitting on a regular expression that is more selective than just splitting on : (it splits on : only if preceded by 2 or more letters):

df = pd.read_csv('data.csv', header=None, sep=',', names=['Result', 'IP', 'Time'])

new_df = df.apply(lambda x: x.str.split('[A-Za-z]{2,}:').str[1].str.strip('\"'))

>>> new_df
    Result       IP                      Time
0  Success  0.0.0.0  2018-08-20T12:00:00.000Z
1  Failure  1.1.1.1  2018-08-20T12:01:00.000Z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM