Extract specified area from DataFrame

Roby

i want to manipulate the following cvs file:

"Day" "Hour" "X1" "X2" "X3" "X4" "X5"
2015-01-01 00:00 1 2 3 4 5
         .....

to the following:

"Day Hour" "X2" "X3" "X5"
"2015-01-01 00:00" 2 3 5
         .....

It's just combine two columns and use a range of columns. Ive tried to following:

csv = pandas.read_csv('test.csv')
csv['Time'] = cvs.Day + " " + csv.Hour
csv.set_index('Time')

I can not figure out how to get this columns without creating a new DataFrame.

Padraic Cunningham

You can reassign csv to a new dataframe:

df['Time'] = df.Day + " " + df.Hour

df = df[[-1]]

Once you have no other reference to the df then it will be gc'd

Or use the csv lib to read and join the columns after zipping with transposing with itertools.izip :

import pandas as pd
from itertools import izip
import csv

with open("foo.csv") as f:
    next(f) # skip header
    r = csv.reader(f)
    zp = izip(*r)
    pairs = izip(next(zp), next(zp))
    df = pd.DataFrame(("{} {}".format(a,b) for a,b in pairs),columns=["Time"])

    print(df)

Output:

              Time
0  2015-01-01 00:00

If you actually want to keep the other columns just drop after creating the new column:

df['Time'] = df.Day + " " + df.Hour

df.drop(["Day","Hour"],axis=1,inplace=True)
print(df)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract area from OpenStreetMap

From Dev

Extract area from OpenStreetMap

From Dev

Extract a dataframe from a dataframe

From Dev

Create bitmap from specified screen area

From Dev

Extract a column from a DataFrame

From Dev

Extract Strings from Dataframe

From Dev

opencv extract path (centerline) from arbitrary area

From Dev

Extract Bytes At specified location from byte array

From Dev

Parsing xml attribute value from specified node area

From Dev

Extract substring from string in dataframe

From Java

Extract object to a dataframe from a list

From Dev

Extract the data from the sublist into dataframe

From Dev

pandas extract list from dataframe

From Dev

extract values from pandas dataframe

From Dev

How to extract value from a dataframe

From Dev

start pandas dataframe index from specified number

From Dev

Extract word from specified string with regex delimited by tab

From Dev

awk: how to extract from file A the columns with indexes specified in file B?

From Dev

Extract elements from specified column for each row in vector

From Dev

How to extract unique values from an array EXCEPT specified values?

From Dev

Extract elements from specified column for each row in vector

From Dev

Extract word from specified string with regex delimited by tab

From Dev

How can I extract specified values from a string in excel document?

From Dev

Extract the number from an element of a specified div class - Beautifulsoup

From Dev

Extract list of subwords from a dataframe without corpus

From Dev

How to extract tuples from a pandas symmetric dataframe

From Dev

Pandas: Iteratively Extract Numpy Arrays From DataFrame

From Dev

Extract string from cell in Pandas dataframe

From Dev

Extract value from single row of pandas DataFrame

Related Related

  1. 1

    Extract area from OpenStreetMap

  2. 2

    Extract area from OpenStreetMap

  3. 3

    Extract a dataframe from a dataframe

  4. 4

    Create bitmap from specified screen area

  5. 5

    Extract a column from a DataFrame

  6. 6

    Extract Strings from Dataframe

  7. 7

    opencv extract path (centerline) from arbitrary area

  8. 8

    Extract Bytes At specified location from byte array

  9. 9

    Parsing xml attribute value from specified node area

  10. 10

    Extract substring from string in dataframe

  11. 11

    Extract object to a dataframe from a list

  12. 12

    Extract the data from the sublist into dataframe

  13. 13

    pandas extract list from dataframe

  14. 14

    extract values from pandas dataframe

  15. 15

    How to extract value from a dataframe

  16. 16

    start pandas dataframe index from specified number

  17. 17

    Extract word from specified string with regex delimited by tab

  18. 18

    awk: how to extract from file A the columns with indexes specified in file B?

  19. 19

    Extract elements from specified column for each row in vector

  20. 20

    How to extract unique values from an array EXCEPT specified values?

  21. 21

    Extract elements from specified column for each row in vector

  22. 22

    Extract word from specified string with regex delimited by tab

  23. 23

    How can I extract specified values from a string in excel document?

  24. 24

    Extract the number from an element of a specified div class - Beautifulsoup

  25. 25

    Extract list of subwords from a dataframe without corpus

  26. 26

    How to extract tuples from a pandas symmetric dataframe

  27. 27

    Pandas: Iteratively Extract Numpy Arrays From DataFrame

  28. 28

    Extract string from cell in Pandas dataframe

  29. 29

    Extract value from single row of pandas DataFrame

HotTag

Archive