Я полностью потерял следующее. У меня есть тестовый Dataframe, заполненный твитами и метаданными. Теперь, при определенных условиях (например, я хочу выбрать все ретвиты), я хочу скопировать строку и записать ее в новый CSV.Python | Выбор строк в dataframe pandas
Дело в том, что я не понимаю, как выбирать строки в Pandas, я консультировался с документацией, но это все еще озадачивает меня. Я пробовал .loc и .ix, но я думаю, что я делаю это неправильно. Поэтому моя идея заключалась в том, чтобы добавлять рябины, а затем индексировать на основе этих рябинов с помощью счетчика и .ix. Поскольку мой индекс, таким образом, является целым числом, я полагал, что это может сработать:
selectRow = file_df.ix[counter,:]
кроме этого нет. Любые советы о том, как выбрать целую строку? Я, вероятно, пропустил что-то супер легкое.
Общий код: # Скрипт принимает твиты и выбирает ретвиты, распечатывая всю строку до нового файла.
import pandas as pd
import string
print("Loading file & initializing variables.")
# load file
file_df = pd.read_csv("Desktop/tweetsamples.csv", delimiter=";")
#declare stuff we need to use
output_df = pd.DataFrame()
rowToCopy = pd.Series()
selectRow = pd.Series()
withoutPuncSeries = pd.Series()
counter = 0
retweet = False
username = ""
print("Working.. Please be patient.")
# define for loop which checks if there is a retweet in the tweet
content = file_df["header"]
splitContent = [content.str.split()] #initialize list
for wordsLists in splitContent:
counter = counter + 1
for wordsList in wordsLists:
if wordsList[0] == "RT":
retweet = True
username = wordsList[1]
withoutPunctuation = "" #initialize/reset placeholder string
for char in username: #we want to get rid of potential interpunction errors behind the username, so we loop through the string
if char != "@": #we don't want to have the @
if char == "_" or char not in string.punctuation: #only desired characters ('_' is a valid char in an username)
withoutPunctuation = withoutPunctuation + char.lower() #add to placeholder string
print "Found retweet from:", withoutPunctuation
withoutPuncSeries = [withoutPunctuation]
selectRow = file_df.ix[counter,:]
rowToCopy = [selectRow, withoutPuncSeries]
output_df = output_df.append(rowToCopy)
rowToCopy = pd.Series() #reset
withoutPuncSeries = pd.Series()
output_df.to_csv("Desktop/retweet test.csv", sep=";")
print("Done.")
Можете ли вы предоставить [MCVE]? – IanS
Как вы хотите выбрать всю строку? –