Skip to main content
Solved

.csv file Ingestion into CDF using Python SDK [Community Contributed]


Forum|alt.badge.img+1

hello community, 

can anyone tell me is there any way to upload .csv files to CDF using Python SDK.

Best answer by roman.chesnokov

@Viswanadha Sai Akhil Pujyam to insert CSV as a RAW table, you can use the following snippet:

import numpy as np
import pandas as pd
from cognite.client import CogniteClient

DATABASE_NAME = "db"
TABLE_NAME = "table"
CSV_FILENAME = "source.csv"

client = CogniteClient()

client.raw.databases.create(DATABASE_NAME)
client.raw.tables.create(DATABASE_NAME, TABLE_NAME)

df = pd.read_csv(CSV_FILENAME, index_col=0).fillna('')

client.raw.rows.insert_dataframe(DATABASE_NAME, TABLE_NAME, df)

 

View original
Did this topic help you find an answer to your question?

4 replies

Forum|alt.badge.img

To upload an actual file, check out:

https://cognite-sdk-python.readthedocs-hosted.com/en/latest/cognite.html#upload-a-file-or-directory

 

Or do you want to read the data and upload for example the rows as assets?


roman.chesnokov
Seasoned Practitioner
  • Seasoned Practitioner
  • 56 replies
  • Answer
  • May 24, 2023

@Viswanadha Sai Akhil Pujyam to insert CSV as a RAW table, you can use the following snippet:

import numpy as np
import pandas as pd
from cognite.client import CogniteClient

DATABASE_NAME = "db"
TABLE_NAME = "table"
CSV_FILENAME = "source.csv"

client = CogniteClient()

client.raw.databases.create(DATABASE_NAME)
client.raw.tables.create(DATABASE_NAME, TABLE_NAME)

df = pd.read_csv(CSV_FILENAME, index_col=0).fillna('')

client.raw.rows.insert_dataframe(DATABASE_NAME, TABLE_NAME, df)

 


Forum|alt.badge.img+1

thank you roman_chesnokov


Dilini Fernando
Seasoned Practitioner
Forum|alt.badge.img+2

Hi @Viswanadha Sai Akhil Pujyam,

We appreciate your contribution to our community hub! We have chosen to move your article to our hub's How-To section as it will greatly benefit other members of our community. Thank you for your understanding, and we look forward to seeing more great contributions from you in the future! 

Best regards,
Dilini 

 


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings