Home > Enterprise >  How to fix Data Frame in Python Pandas
How to fix Data Frame in Python Pandas

Time:08-05

I just started learning the Pandas package in Python and I need help.

I have a csv file and I want to read it using function pd.read_csv('...') and I want to get a Data Frame in Pandas. After using construction df = pd.read_csv('MyFile.csv') df1 = pd.DataFrame(df) I got:

       rowCount;probingTimestamp

0  315349655;2020-01-13 09:33:00

1  315349655;2020-01-13 09:34:00

2  314533770;2020-01-11 01:18:00 

3  289249872;2020-04-05 16:10:00 

4  289249872;2020-04-05 16:11:00

and when I want to find shape of this DataFrame print(df1.shape) I got:

(5, 1)

So there are one column and five rows.

Also I can use a loop and make 3 columns from 1 column, but I need to create a new table and this will take up a lot of memory and execution time of the function.

Maybe there is an alternative way in the Pandas library or it can be done only through a loop? If not, how can I fix it using loops in Python?

CodePudding user response:

I think you need sep=;

df = pd.read_csv('MyFile.csv',sep=';')
print (df)
     rowCount       probingTimestamp
0  315349655  2020-01-13 09:33:00
1  315349655  2020-01-13 09:34:00
2  314533770  2020-01-11 01:18:00
3  289249872  2020-04-05 16:10:00
4  289249872  2020-04-05 16:11:00

print (df.shape)
(5, 2)

If you want DataFrame from Series

print (pd.DataFrame(df['rowCount']))
     rowCount
0  315349655
1  315349655
2  314533770
3  289249872
4  289249872

print (pd.DataFrame(df['rowCount']).shape
(5, 1)
  • Related