python - Pandas removing rows -
i'd remove few rows in csv file pandas pivot table function. format of csv file looks this:
ad dsdsa d adsadsa ewew das ds dsd sad dsa dsa 1 1 1.0 3 1 2 2.0 4 1 3 3.2 4 1 4 2.3 5 1 5 1.3 2 2 1 2.2 5 2 2 3.2 5 2 3 2.2 4 2 4 1.1 4
i want output this:
1 2 3 4 5 1 1.0 2.0 3.2 2.3 1.3 2 2.2 5 3.2 1.1 . 3 . . . . .
i have trick without rubbish data in first 2 rows:
import pandas pd df = pd.read_csv('third.csv', usecols=[0,1,2], names=['origin','destin', 'value']) pd.pivot_table(df, index='origin', columns='destin', values='value') pd.to.excel('test.xlsx', sheet_name='sheet1', index='true')
as can see, used first 3 columns, i'd remove first 2 rows in csv file. also, i'd export new format excel (but last row of code doesn't work).
any thoughts go this?
demo
from io import stringio import pandas pd txt = """ad dsdsa d adsadsa ewew das ds dsd sad dsa dsa 1 1 1.0 3 1 2 2.0 4 1 3 3.2 4 1 4 2.3 5 1 5 1.3 2 2 1 2.2 5 2 2 3.2 5 2 3 2.2 4 2 4 1.1 4""" df = pd.read_csv(stringio(txt), delim_whitespace=true, skiprows=2, header=none, index_col=[0, 1], usecols=[0, 1, 2], squeeze=true) df.unstack()
if names on indices bother you:
df.rename_axis([none, none]).unstack()
Comments
Post a Comment