Showing posts with label csv. Show all posts
Showing posts with label csv. Show all posts

12/10/2021

mongodb python, Table recode export, loading, save csv, load csv

 refer to below code:


.

#connet db
client = pymongo.MongoClient("mongodb://localhost:27017/")
#connet table
who_table = db["who_table"]

#delete all
who_table.drop()

#load csv to table
df = pd.read_csv('1.csv')
data = df.to_dict('records')
who_table.insert_many(data)
#show all items
for x in who_table.find():
print(x)

#save table to csv
cursor = who_table.find({})
df = pd.DataFrame(list(cursor))
df.to_csv('2.csv', index=False)

.


Thank you.🙇🏻‍♂️

www.marearts.com


8/20/2018

python numpy to CSV, numpy to pandas, pandas to CSV

Sample code for
- numpy to CSV
- numpy to pandas
- pandas to CSV



import numpy as np
import pandas as pd

f1_numpy = "./data/test1.csv"
f2_pandas = "./data/test2.csv"

#numpy to csv
np.savetxt(f1_numpy, np.array([10,20]))
print(f1_numpy)

#numpy to pandas and csv
pda = pd.DataFrame(np.array([10,20]), columns=['data'])
pda.to_csv(f2_pandas, index=False)

Thank you.

8/10/2018

3D array numpy -> pandas ->csv -> pandas -> 3d array numpy

This article is example source code for
3D array numpy -> pandas -> csv -> pandas -> 3D array numpy

Let's see step by step


Step 1, make example data

import numpy as np
import pandas as pd


#make list
a = [[11, 12, 13, 14, 15], [15, 16, 17, 18, 19]]
b = [[21, 22, 23, 24, 25], [25, 26, 27, 28, 29]]
c = []
c.append(a)
c.append(b)
#make numpy
npa = np.array(c)
print('npa\n',npa)
print('npa shape\n',npa.shape) #2 by 2 by 5


result
npa
 [[[11 12 13 14 15]
  [15 16 17 18 19]]

 [[21 22 23 24 25]
  [25 26 27 28 29]]]
npa shape
 (2, 2, 5)


Step 2, numpy to pandas
#make numpy to panda
m,n,r = npa.shape
#numpy ->group indexing, reshape
out_arr = np.column_stack((np.repeat(np.arange(m),n),npa.reshape(m*n,-1)))
out_df = pd.DataFrame(out_arr, columns=['group','a','b','c','d','e'])
print('pnadas\n',out_df) #pandas

result

group   a   b   c   d   e
0      0  11  12  13  14  15
1      0  15  16  17  18  19
2      1  21  22  23  24  25
3      1  25  26  27  28  29


Step 3, save csv, load csv

#save to csv
out_df.to_csv('test3Dpandas.csv', index=False)
#load csv
df = pd.read_csv('test3Dpandas.csv')


Step 4, pandas to numpy

#pandas to numpy
npb = df.values
npb = npb[:,1:]
npb2 = npb.reshape(m,n,r)
print('numpy\n',npb2)

result

numpy
 [[[11 12 13 14 15]
  [15 16 17 18 19]]

 [[21 22 23 24 25]
  [25 26 27 28 29]]]







2/04/2018

python csv file data simple handler.

I made simple class witch is to handle element datas in csv file.

Basically, we can get elements by indexing.
For example) Get first line row data, or 2~3 line row data.

And we also can get some part of elements in csv.
For example) row 2~3 and col 2~4.

refer to this image




And we can save new csv file.

Actually, I made this class for handle MNIST data file.
And the source code uploaded in GitHub repository.
https://github.com/MareArts/csv_file_handle 
I hope someone updated this code.
Anyone can do contributor.

And I hope to help this code to someone. ^^


.code.

.code.