Search code examples
pythonpandasdataframedate-range

Python Range Problem For Using Jype1 and can not use for loop every column and row


I would like to ask you that i have a data and i would like to call a package. Package is a Jar file type.

Anyway, i have a csv file:

Ürünler01   Ürünler02   Ürünler03   Ürünler04
0   trafik  musavirligi na  na
1   aruba   2930f   48g poe
2   minilink    6363    721l    na
3   rendezvous  point   appliance   na
4   in uzak oku sayaç   na
... ... ... ... ...
79  inbpano kurulum kor panos
80  tn  card    değişim na
81  servis  kapı    kaynaklı    panel
82  evrensel    microwave   outdoor unit
83  hp  ekipman na  na

As you can see column names are : 'Ürünler01', 'Ürünler02', 'Ürünler03', 'Ürünler04'.

And i would like to apply my "message" function and its at below:

new=[]
for message in df['Ürünler01']:
      new.append(clean_messages(message))

after that code i will take it data frame and i can publish it.

df = pd.DataFrame (new)

And result is

df

    0
0   trafik
1   araba
2   minicik
3   rendezvous
4   in uzak
... ...
79  inbpano
80  en
81  servis
82  evrensel
83  hp

AND my question is i can not apply my append "message" function all over Ürünler01,Ürünler02,Ürünler03 and Ürünler04. I could not find iloc or loc and can not understand for usage in python. As i can apply at C programming using i and j for loops and i can do my functions all of rows and columns. But my problem is at this question i can not use my functions all columns.

Please help my situation. I added pictures below. I can print out "0" column but also i need 1,2,3 which are painted on screenshots. I am waiting your helps

enter image description here

enter image description here


Solution

  • The final shape of your dataframe isn't very clear from your question, but you could iterate over the column names (default iteration over a dataframe) and then the rows by indexing the column from the original dataframe by-name

    import pandas as pd
    
    # load dataframe
    df = pd.read_csv("path_to_file.csv")
    
    # start a new string series
    series = pd.Series([], dtype=str)
    for colname in df:               # iterate over the column names
        for message in df[colname]:  # iterate over the rows in the column
            series.append(clean_messages(message))
    
    df_result = pd.DataFrame(series)  # optional, can directly use series
    

    However, you may be able to use df.apply directly to apply clean_messages to every value in your dataframe

    df_result = pd.DataFrame()
    for colname in df:
        df_result[colname] = df[colname].apply(clean_messages)